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;
}
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 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!
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With