2015-04-23 50 views
0

我一直在试图绘制从一个点到另一个点后,他开始他的旅行的用户路径。我使用Google地图iOS SDK和GMSPolyLine来绘制用户开始旅行后的路径。在谷歌地图中跟踪用户路径iOS SDK

上午使用locationManager:didUpdateLocation跟踪旅行,并在用户更新其位置后绘制线条。当我们搜索从一个点到另一个点的路径时,无法正确绘制路径,因为它发生在Google地图中。我添加了屏幕截图,以了解所发生的差异。

我的应用程序: https://www.dropbox.com/s/h1wjedgcszc685g/IMG_6555.png?dl=0

以上是我的应用程序的截图,你可以注意到,转弯不正确绘制

所需的输出: https://www.dropbox.com/s/poqaeadh1g93h6u/IMG_6648.png?dl=0

任何人都可以点我朝着绘制类似于Google地图的整洁路径的最佳做法?

回答

0

发生这种情况的原因是您的位置不会连续更新,并且更新多段线时会在两点之间画直线,因此您必须在您获取下一个位置时调用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]; 

这件事可以帮助你。

+1

在某些情况下,应用程序在characterAtIndex的decodePolyLine方法中崩溃***由于未捕获异常'NSRangeException',原因:' - [__ NSCFString characterAtIndex:]:范围或索引超出范围'终止应用程序'如果在decodePolyLine方法的while循环我改变while(index

+0

@amitgupta谢谢你的建议。我更新我的答案。 –

+1

第二种方法工作正常,但第一种方法在某些情况下崩溃,所以你不能改变这个循环,因为在某些情况下在len-1,len-2基于不同的-2地址工作。所以我的建议是使用第二种方法而不是第一种方法来避免崩溃。 @chirag shah –