0

CLLocation2D有点,,,,并且都MKMapPoints以一种或另一种形式给你点。我正在绘制一条自定义路线:MKCoordinatesconvertCoordinate:toPointInView:

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

绘图的正确点是什么?

4

2 回答 2

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

}

于 2013-05-24T08:46:12.390 回答
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];
}
}
于 2013-05-24T09:17:15.523 回答