2015-04-23 47 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 –