我用什么坐标用MKMapView手动绘制自定义路径?

时间:2013-05-24 08:35:42

标签: ios ios6 mkmapview

CLLocation2D个点,MKMapPointsMKCoordinatesconvertCoordinate:toPointInView:,它们都以某种形式为您提供积分。我正在绘制一条自定义路线:

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

什么是用于绘图的正确点?

2 个答案:

答案 0 :(得分:0)

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

-(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);

}

答案 1 :(得分:0)

您可以使用google api -

在.h -

中定义合适的变量
NSArray *arrRoutePoints;
MKPolygon *objpolygon;
MKPolyline *objpolyline;

调用函数就像你想要的那样 -

self.arrRoutePoints=[self getRoutePointFrom:newSource to:newDestination];
[self drawRoute];

功能代码:

 - (NSArray*)getRoutePointFrom:(MKPointAnnotation *)origin to:(MKPointAnnotation *)destination
{
    NSString *saddress=[NSString stringWithFormat:@"%f,%f",origin.coordinate.latitude,origin.coordinate.longitude];
NSString *daddress=[NSString stringWithFormat:@"%f,%f",destination.coordinate.latitude,destination.coordinate.longitude];

NSString *apiUrlString=[NSString stringWithFormat:@"http://maps.google.com/maps?output=dragdir&saddr=%@&daddr=%@", saddress, daddress];

NSURL *apiUrl=[NSURL URLWithString:apiUrlString];

NSError *error;
NSString *apiResponse=[NSString stringWithContentsOfURL:apiUrl encoding:NSUTF8StringEncoding error:&error];

NSRange range;
range=[apiResponse rangeOfString:@"points:\\\"([^\\\"]*)\\\"" options:NSRegularExpressionSearch];

NSString *encodedPoint=[apiResponse substringWithRange:range];

NSLog(@"Encode lentgh %d",[encodedPoint length]);

encodedPoint=[encodedPoint stringByReplacingOccurrencesOfString:@"points:\"" withString:@""];
encodedPoint=[encodedPoint stringByReplacingOccurrencesOfString:@"\"" withString:@""];

return  [self decodePolyLine:[encodedPoint mutableCopy]];

}


- (NSMutableArray *)decodePolyLine:(NSMutableString *)encodedString
{

[encodedString replaceOccurrencesOfString:@"\\\\" withString:@"\\"
                                  options:NSLiteralSearch
                                    range:NSMakeRange(0, [encodedString length])];
NSInteger len = [encodedString length];
NSInteger index = 0;
NSMutableArray *array = [[NSMutableArray alloc] init];
NSInteger lat=0;
NSInteger lng=0;
while (index < len) {
    NSInteger b;
    NSInteger shift = 0;
    NSInteger result = 0;
    do {

            b = [encodedString characterAtIndex:index++] - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
    } while (b >= 0x20);
    NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
    lat += dlat;
    shift = 0;
    result = 0;
    do {
        if(index<len)
        {
        b = [encodedString characterAtIndex:index++] - 63;
        result |= (b & 0x1f) << shift;
        shift += 5;
        }
        else
        {
            break;
        }
    } while (b >= 0x20);
    NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
    lng += dlng;
    NSNumber *latitude = [[NSNumber alloc] initWithFloat:lat * 1e-5];
    NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5];
    printf("\n[%f,", [latitude doubleValue]);
    printf("%f]", [longitude doubleValue]);
    CLLocation *loc = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];

    [array addObject:loc];
}
return array;
}


- (void)drawRoute
{
int numPoints=[arrRoutePoints count];

if(numPoints >1)
{
    CLLocationCoordinate2D *coord=malloc(numPoints *sizeof(CLLocationCoordinate2D));
    for(int i=0;i<numPoints;i++)
    {
        CLLocation *current=[arrRoutePoints objectAtIndex:i];
        NSLog(@"%@",[arrRoutePoints objectAtIndex:i]);
        NSLog(@"Current \n[%g",current.coordinate.latitude);
         NSLog(@"%g]",current.coordinate.longitude);
        coord[i]=current.coordinate;

        NSLog(@"\n[%g",coord[i].latitude);
        NSLog(@"%g]",coord[i].longitude);

    }
    self.objpolyline=[MKPolyline polylineWithCoordinates:coord count:numPoints];
    free(coord);
    [mapView  addOverlay:objpolyline];
    [mapView setNeedsDisplay];
}
}