我是Xcode的新手,我在使用折线追踪用户路径方面遇到了问题。
我正确地获得了位置。我能够正确添加引脚。但是,从不调用我的折线方法。
以下是我的代码。
在标题文件中......
@interface Tracker : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
{
MKMapView *mapView;
//other declarations
}
在实现文件中,我有以下代码。我在didUpdateLocations方法中调用drawPolyline方法,该方法被正确调用。
- (void) drawPolyline:(NSArray *)locations
{
NSInteger numberOfLocations = [locations count];
if (numberOfLocations > 1)
{
CLLocationCoordinate2D *locationCoordinate2DArray = malloc(numberOfLocations * sizeof(CLLocationCoordinate2D));
for (int i = 0; i < numberOfLocations; i++)
{
CLLocation* current = [locations objectAtIndex:i];
locationCoordinate2DArray[i] = current.coordinate;
}
self.polyline = [MKPolyline polylineWithCoordinates:locationCoordinate2DArray count:numberOfLocations];
free(locationCoordinate2DArray);
[mapView addOverlay:self.polyline];
[mapView setNeedsDisplay];
}
}
- (MKOverlayView*)mapView:(MKMapView*)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKPolylineView* polyLineView = [[MKPolylineView alloc] initWithPolyline:self.polyline];
polyLineView.strokeColor = [UIColor blueColor];
polyLineView.lineWidth = 2;
return polyLineView;
}
非常感谢您的帮助。提前谢谢。
答案 0 :(得分:0)
mapView:viewForOverlay:
是委托方法,因此您需要在某处设置mapview的委托。否则,永远不会调用委托方法。
[mapView setDelegate:self];