Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MKMapView - rendererForOverlay not called

I recently started learning objectiveC and started developing an app in iOS6.

Now, I am trying to convert it for iOS7 and facing issues with MKMap.

In iOS6, I was using viewForOverlay.

In iOS7, I am changing it to renderForOverlay. But, my application is not calling mapView:rendererForOverlay. Below is my code. Appreciate your help.

- (void) drawPolyline:(NSArray *)locations
{
    [mapView setDelegate:self];
    ...
    ...

    self.polyline = [MKPolyline polylineWithCoordinates:locationCoordinate2DArray count:numberOfLocations];
    free(locationCoordinate2DArray);
    [mapView addOverlay:self.polyline];
    [mapView setNeedsDisplay];
}

- (MKOverlayRenderer*)mapView:(MKMapView*)mapView rendererForOverlay:(id <MKOverlay>)overlay
{
    MKPolylineRenderer* lineView = [[MKPolylineRenderer alloc] initWithPolyline:self.polyline];
    lineView.strokeColor = [UIColor blueColor];
    lineView.lineWidth = 7;
    return lineView;
}
like image 537
user2734323 Avatar asked Sep 20 '13 00:09

user2734323


3 Answers

I am assuming that you did declare the MKMapViewDelegate delegate in your header file via the @interface statement:

However, did you assign the delegate in the viewDidLoad (or where you think its appropriate) method?

self.mapView.delegate = self;
like image 106
Mike Petrogeorge Avatar answered Oct 31 '22 20:10

Mike Petrogeorge


Like others have said, be sure you set your delegate before adding them. Use the addOverlay:level: method since addOverlay: will be deprecated (according to the comment in the header).

My issue was something dumb. I had lat and long switched by mistake for my polygon's points. Be sure to double check that if they still aren't showing up.

You might also try logging pointCount on your polygon to make sure they are being set properly.

Related, this is how to do it in Swift:

// Get your coordinates from somewhere as an [CLLocationCoordinate2D] array
// If you already have them, make a local mutable copy
var coordinates = [CLLocationCoordinate2D]()

// Create your polygon
let polygon = MKPolygon(coordinates: &coordinates, count: coordinates.count)

Hopefully this saves you some time!

like image 2
Sam Soffes Avatar answered Oct 31 '22 19:10

Sam Soffes


OK, I had the same issue and finally found the case. We have to use [MKMapView addOverlay: level:] instead of [MKMapView addOverlay:]. It triggers rendererForOverlay rather than viewForOverlay of the delegate. Hope this would be helpful for you iOS 7 lovers!

like image 1
Pei Avatar answered Oct 31 '22 19:10

Pei