Search code examples
iosios6mkmapview

With what coordinates do i use to manually draw a custom path with an MKMapView?


There are CLLocation2D points, MKMapPoints, MKCoordinates, and convertCoordinate:toPointInView: which all give you points in one form or another. I am drawing a custom route in:

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

Whats the proper point to use for drawing?


Solution

  •     routeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, mapView.frame.size.width, mapView.frame.size.height)];
        routeView.userInteractionEnabled = NO;
        [mapView addSubview:routeView];
    

    And

    -(void) updateRouteView {
    CGContextRef context =  CGBitmapContextCreate(nil, 
                                                       routeView.frame.size.width, 
                                                       routeView.frame.size.height, 
                                                  8, 
                                                  4 * routeView.frame.size.width,
                                                  CGColorSpaceCreateDeviceRGB(),
                                                  kCGImageAlphaPremultipliedLast);
    
     CGContextSetStrokeColorWithColor(context, lineColor.CGColor);
     CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
     CGContextSetLineWidth(context, 4.0);
    
    for(int i = 0; i < self.routes.count; i++) {
        CLLocation* location = [self.routes objectAtIndex:i];
        CGPoint point = [mapView convertCoordinate:location.coordinate toPointToView:routeView];
    
        if(i == 0) {
            CGContextMoveToPoint(context, point.x, routeView.frame.size.height - point.y);
        } else {
            CGContextAddLineToPoint(context, point.x, routeView.frame.size.height - point.y);
        }
    }
    
    CGContextStrokePath(context);
    
    CGImageRef image = CGBitmapContextCreateImage(context);
    UIImage* img = [UIImage imageWithCGImage:image];
    
    routeView.image = img;
    CGContextRelease(context);
    

    }