使用MKPolyline绘制两个位置之间的路径

Vir*_*ani 3 iphone ios

我试图在教程的帮助下显示两个位置之间的路线.他们使用了坐标的CSV文件,我使用谷歌api来获取坐标.但结果完全不同. 产量

你可以看到它没有绘制正确的路径.Plz建议我一些事情.

Ank*_*ava 14

你需要解码你从响应中得到的折线..为了你需要谷歌的算法......

// http://code.google.com/apis/maps/documentation/utilities/polylinealgorithm.html  
//  
-(NSMutableArray *)decodePolyLine:(NSString *)encodedStr {  
    NSMutableString *encoded = [[NSMutableString alloc] initWithCapacity:[encodedStr length]];  
    [encoded appendString:encodedStr];  
    [encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"  
                                options:NSLiteralSearch  
                                  range:NSMakeRange(0, [encoded length])];  
    NSInteger len = [encoded length];  
    NSInteger index = 0;  
    NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];  
    NSInteger lat=0;  
    NSInteger lng=0;  
    while (index < len) {  
        NSInteger b;  
        NSInteger shift = 0;  
        NSInteger result = 0;  
        do {  
            b = [encoded 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 {  
            b = [encoded characterAtIndex:index++] - 63;  
            result |= (b & 0x1f) << shift;  
            shift += 5;  
        } while (b >= 0x20);  
        NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));  
        lng += dlng;  
        NSNumber *latitude = [[[NSNumber alloc] initWithFloat:lat * 1e-5] autorelease];  
        NSNumber *longitude = [[[NSNumber alloc] initWithFloat:lng * 1e-5] autorelease];  
        //          printf("[%f,", [latitude doubleValue]);  
        //          printf("%f]", [longitude doubleValue]);  
        CLLocation *loc = [[[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]] autorelease];  
        [array addObject:loc];  
    }  
    [encoded release];  
    return array;  
}
Run Code Online (Sandbox Code Playgroud)

这将为您提供包含所有点的可变数组(以CLLocation对象的形式),也不仅仅解码主折线..解码您收到的每个子折线,否则绘制方向将不合适.

  • 我使用相同的编码功能,但应用程序崩溃.在删除[encoded replaceOccurrencesOfString:@"\\\\"withString:@"\\"options:NSLiteralSearch range:NSMakeRange(0,[encoded length])]; 它工作但它没有给出准确的结果. (4认同)