Reputation: 706
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;
}
Upvotes: 14
Views: 13311
Reputation: 1279
Just a hint for such like me, which struggle with IOS 13 on that. It looks, as IOS 13 ignores something like:
// self is a MKTileOverlayRenderer
// the code is called, if a method has produced a tile for a given MKMapRect
// tell the system that we are ready
DispatchQueue.main.async(execute: {
// invalidate the mapRect
self.setNeedsDisplay(mapRect)
})
This code worked fine in IOS 10 - IOS 12, but now you have to add the zoomScale to the setNeedsDisplay(), otherwise it seems to be ignored by IOS 13... cost me 4 hours to sort it out ;-)
self.setNeedsDisplay(mapRect, zoomScale: zoomScale)
Upvotes: 0
Reputation: 14935
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!
Upvotes: 2
Reputation: 4232
For me, the solution involved two steps:
Upvotes: 0
Reputation: 11
I just finished my experiments with this method and I found that only nib-file placed MKMapView and @property (weak, nonatomic) IBOutlet MKMapView *mapView;
fixed this issue. Also here is checklist.
Upvotes: 0
Reputation: 11
If there is only one point in locationCoordinate2DArray,mapView:rendererForOverlay
would not be called.
Upvotes: 1
Reputation: 11643
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!
Upvotes: 1
Reputation: 171
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;
Upvotes: 17