问题描述
我试图在教程的帮助下显示两个地点之间的路线。他们使用了坐标的CSV文件,我使用谷歌api来获取坐标。但结果完全不同。
I am trying to show route between two locations with the help of this tutorial. They have used a CSV file of coordinates and i am using google api to get coordinates. But Result is completely different.
如你所见它没有绘制正确的路径。
Plz建议我。
as you can see it is not drawing correct path.Plz suggest me something.
推荐答案
您需要解码从响应中得到的折线。并且为了你需要谷歌的算法......
You need to the decode the polyline that you are getting from the response.. and in order to that you need google's algorithm...
// 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;
}
这将为您提供包含所有点的可变数组(以CLLocation对象)
并且也不要只解码主折线..解码你收到的每一个子折线,否则说明方向不正确。
this will give you the mutable array with all the points (in the form of CLLocation objects)and also dont just decode the main polyline .. decode each and every sub polyline that you receive and than plot otherwise the directions will not be proper.
这篇关于使用MKPolyline绘制两个位置之间的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!