2

I am working with iOS 7 and have extended an MKOverlayPathRenderer in order to draw custom overlays on an MKMap.

I have implemented the following method of the MKOverlayPathRenderer class and the NSLog output shows that the code is called for drawing the context when I would expect. However, the custom overlays are simply NOT showing on the Map.

---CustomPathRenderer extends MKOverlayPathRenderer----

    -(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)ctx
        {

            CGContextSetAlpha(ctx, 0.9);

            if([self.overlay isKindOfClass:[CustomPathOverlay class]]){

                CustomPathOverlay *path = (CustomPathOverlay *)self.overlay;

                for(int idx = 1; idx < path.selected.positions.count; idx++)
                {
                    CGContextBeginPath(ctx);

                    Position* firstPosition = (Position *)[path.selected.positions objectAtIndex:idx-1];

                    CGPoint firstpoint = [self.mapView convertCoordinate:firstPosition.location toPointToView:self.mapView];

                    CGContextMoveToPoint(ctx, firstpoint.x , firstpoint.y);

                    Position* pos = (Position *)[flightPath.selectedAircraft.positions objectAtIndex:idx];

                    CGPoint point = [self.mapView convertCoordinate:pos.location toPointToView:self.mapView];

                    NSLog(@"Drawing X:%f Y:%f",point.x,point.y);

                    CGContextAddLineToPoint(ctx, point.x, point.y);

                    CGContextSetStrokeColorWithColor(ctx,[UIColor redColor].CGColor);<-- THIS WILL BECOME A FUNCTION OF A POSITION PROPERTY RATHER THAN JUST RED.

                    CGContextSetLineWidth(ctx, 0.5 * MKRoadWidthAtZoomScale(zoomScale));

                    CGContextStrokePath(ctx);
                }

                // Last segment to the last known position.
                CGContextBeginPath(ctx);

                Position* lastPosition = (Position *)[path.selected.positions lastObject];

                CGPoint lastPoint = [_mapView convertCoordinate:lastPosition.location toPointToView:self.mapView];

                CGContextMoveToPoint(ctx, lastPoint.x , lastPoint.y);

                Position *estimatedPosition = [path.selected.position estimatePosition];

                CGPoint endPoint = [self.mapView convertCoordinate:estimatedPosition.location toPointToView:self.mapView];

                CGContextAddLineToPoint(ctx, endPoint.x, endPoint.y);

                CGContextSetStrokeColorWithColor(ctx,[UIColor redColor].CGColor); 

                CGContextSetLineWidth(ctx, 0.5 * MKRoadWidthAtZoomScale(zoomScale));

                CGContextStrokePath(ctx);


            }

    }

Here is the code from the (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay method of my MKMapViewDelegate:

    - (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay
{

    if([overlay isKindOfClass:[CustomPathOverlay class]]){
        CustomPathRenderer *renderer = [[CustomPathRenderer alloc]initWithOverlay:overlay andMapView:mapView];

        return renderer;
    }

    return nil;

}

I would really appreciate some clues as to why this might not be rendering on the MKMapView.

Many thanks

totalcruise
  • 1,343
  • 2
  • 13
  • 25
  • 1
    Instead of converting the coordinates to CGPoints in the _view_ using convertCoordinate:toPointToView:, I believe you need to use the `pointForMapPoint:` method which gives a CGPoint in the overlay's drawing context (you'll need to first convert the coordinate to an MKMapPoint using the MKMapPointForCoordinate function). –  Nov 08 '13 at 21:51

0 Answers0