Use MKOverlay and MKOverlayRenderer

Posted in code with : ios


MKOverlay

Write your own overlay class (for example, MapOverlay) confirm to MKOverlay, synthesize coordinate and boundingMapRect, so that we can assign value to them while initializing it.

Note that the type of coordinate is CLLocationCoordinate2D with latitude and longitude, while boundingMapRect is MKMapRect. And we need to use MKMapPointForCoordinate method to convert a CLLocationCoordinate2D type data to MKMapPoint type data. If we assign the CLLocationCoordinate2D data directly to boundingMapRect, the overlay will be too small to draw, and the mapView:rendererForOverlay: method will not be called at all. It is very hard for debugging.

Sample code:

 1 @implementation MapOverlay
 2 
 3 @synthesize coordinate;
 4 @synthesize boundingMapRect;
 5 
 6 - (instancetype)init
 7 {
 8     self = [super init];
 9     if (self) {
10         CGFloat radius = 10.0;
11         CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(37.0, -111.0);
12         coordinate = CLLocationCoordinate2DMake(coords.latitude, coords.longitude);
13         
14         MKMapPoint startMapPoint = MKMapPointForCoordinate(CLLocationCoordinate2DMake(coords.latitude - radius, coords.longitude - radius));
15         MKMapPoint endMapPoint = MKMapPointForCoordinate(CLLocationCoordinate2DMake(coords.latitude + radius, coords.longitude + radius));
16         boundingMapRect = MKMapRectMake(startMapPoint.x, startMapPoint.y, fabs(endMapPoint.x - startMapPoint.x), fabs(endMapPoint.y - startMapPoint.y));
17     }
18 
19 	return self;
20 }
21 
22 @end

MKOverlayRenderer

Write your own overlay renderer class (for example, MapOverlayRenderer) confirm to MKOverlayRenderer, and implement drawMapRect:zoomScale:inContext: method.

Sample code:

1 - (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
2 {
3     MKMapRect overlayMapRect = self.overlay.boundingMapRect;
4     CGRect drawRect = [self rectForMapRect:overlayMapRect];
5     CGContextSetRGBFillColor(context, 0, 0, 1, 0.4);
6     CGContextFillRect(context, drawRect);
7 }

Interact with MKMapView

After you implement your overlay and overlay renderer, implement mapView:redererForOverlay method in your map view’s delegate. In this method, you need to return an instance of your own overlay renderer, initialized with the given overlay.

Sample code:

 1 - (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
 2 {
 3     if ([overlay isKindOfClass:[MapOverlay class]]) {
 4         MapOverlayRenderer *overlayRenderer = [[MapOverlayRenderer alloc] initWithOverlay:overlay];
 5         
 6         return overlayRenderer;
 7     }
 8     
 9     return nil;
10 }

Then, we can add overlay instance to the map view through addOverlay or addOverlays methods.

Sample code:

1 MapOverlay *overlay = [[MapOverlay alloc] init];
2 [self.mapView addOverlay:overlay level:MKOverlayLevelAboveLabels];