I am very new to Xcode and I am facing issues in tracing user path using polyline.
I am getting locations correctly. I am able to add pins properly. However, my polyline method is never called.
Below is my code.
In header file...
@interface Tracker : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
{
MKMapView *mapView;
//other declarations
}
Inside implementation file, I have following code. I call, drawPolyline method inside didUpdateLocations method, which is called correctly.
- (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;
}
Your help is highly appreciated. Thanks in advance.
mapView:viewForOverlay:
is a delegate method, so you'll need to set the mapview's delegate somewhere. Otherwise, the delegate method will never be called.
[mapView setDelegate:self];