Why rendererForOverlay for MapKit isn't being called?

I am trying to run CLLocation with latitude and longitude. Then add it to NSMutableArray named trackPointArray

.

After that in the loop I have 18 CLLocation

insidecurrLocation

CLLocation *currLocation = [[CLLocation alloc] initWithLatitude:lastlang longitude:lastlong];
[trackPointArray addObject:currLocation];

      

After adding to this array, I create CLLocationCoordinate2D

NSInteger numberOfSteps =trackPointArray.count;
CLLocationCoordinate2D coordinates[numberOfSteps];
for(NSInteger index=0;index<numberOfSteps;index++)
{
    CLLocation *location = [trackPointArray objectAtIndex:index];
    CLLocationCoordinate2D coordinate2 = location.coordinate;
    coordinates[index] = coordinate2;
}

      

Finally, I call this:

routeLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
[self.mapView addOverlay:routeLine];

      

And this function rendererForOverlay

:

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    if (![overlay isKindOfClass:[MKPolygon class]]) {
        return nil;
    }
    MKPolygon *polygon = (MKPolygon *)overlay;
    MKPolygonRenderer *renderer = [[MKPolygonRenderer alloc] initWithPolygon:polygon];
    renderer.fillColor = [[UIColor darkGrayColor] colorWithAlphaComponent:0.4];
    return renderer;
}

      

  • I was debugging my project and didn't call rendererForOverlay

  • And I have this line of code after viewDidLoad()

    self.mapView.delegate = self;
  • I have more than one item
  • And here is the definition of my delegate in the header file:

    UIViewController <MKMapViewDelegate, CLLocationManagerDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, MKOverlay>

5. MapView

=

@property (weak, nonatomic) IBOutlet MKMapView *mapView;

      

  1. I am trying this on a simulator. And the simulator brings l

Here is the definition of my variables:

NSMutableArray *trackPointArray;
MKMapRect routeRect;
MKPolylineView* routeLineView;
MKPolyline* routeLine;

      

Also I used this older version, but it doesn't get called either:

-(MKOverlayView *) mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
    MKPolylineView *polylineview = [[MKPolylineView alloc] initWithPolyline:overlay];
    polylineview.strokeColor = [UIColor redColor];
    polylineview.lineWidth = 5.0;
    return polylineview;
}

      

I appreciate if anyone can help me with this.

Thanks to Anna for her comment, I think this will fix this issue:

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolyline *route = overlay;
        MKPolylineRenderer *routeRenderer = [[MKPolylineRenderer alloc] initWithPolyline:route];
        routeRenderer.strokeColor = [UIColor blueColor];
        return routeRenderer;
    }
    else return nil;
}

      

+3


source to share





All Articles