发生这种情况的原因是您的位置不会连续更新,并且更新多段线时会在两点之间画直线,因此您必须在您获取下一个位置时调用this api。
当你调用这个api时,你得到了你从你获得最佳路线(可能的第一条路线)通过的两点之间的路线数组。从该路由字典中提取对象的overview_polyline对象。 overview_polyline对象是您的两点之间的位置点的纬度和经度数组。
当您通过以下方法进行转换,那么你必须要
有两种方法来解码折线确切折线
第一种方法
#pragma mark
#pragma mark - decode polyline
// these function is given by client
-(void) decodePoly:(NSString *)encoded Color:(UIColor *)color
{
GMSMutablePath *path = [[GMSMutablePath alloc] init];
// NSString *[email protected]"g|vfEmo{[email protected]@[email protected]@[email protected][email protected]@[email protected]][email protected]^[email protected]@";
NSUInteger index = 0, len = encoded.length;
int lat = 0, lng = 0;
while (index < (len - 2)) {
int b, shift = 0, result = 0;
do {
// b = encoded.charAt(index++) - 63;
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(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);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
CLLocation *loc = [[CLLocation alloc] initWithLatitude:((double) lat/1E5) longitude:((double) lng/1E5)];
[path addCoordinate:loc.coordinate];
}
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
// Add the polyline to the map.
polyline.strokeColor = color;
polyline.strokeWidth = 5.0f;
polyline.map = [self getGoogleMap];
}
二方法
GMSPolyline *polyline =[GMSPolyline polylineWithPath:[GMSPath pathFromEncodedPath:"your encoded string"]];
// Add the polyline to the map.
polyline.strokeColor = color;
polyline.strokeWidth = 5.0f;
polyline.map = [self getGoogleMap];
这件事可以帮助你。
在某些情况下,应用程序在characterAtIndex的decodePolyLine方法中崩溃***由于未捕获异常'NSRangeException',原因:' - [__ NSCFString characterAtIndex:]:范围或索引超出范围'终止应用程序'如果在decodePolyLine方法的while循环我改变while(index
@amitgupta谢谢你的建议。我更新我的答案。 –
第二种方法工作正常,但第一种方法在某些情况下崩溃,所以你不能改变这个循环,因为在某些情况下在len-1,len-2基于不同的-2地址工作。所以我的建议是使用第二种方法而不是第一种方法来避免崩溃。 @chirag shah –