0

請參閱下面有關google方向API響應的參考。在monodroid C#中編寫Google API Polyline點編號

https://developers.google.com/maps/documentation/directions/?csw=1#JSON

,你可以看到每個step標籤包括一個名爲polyline標籤。這個標籤包含一個名爲points的子標籤。據我所知,這個標籤包括了你需要在地圖上繪製這個方向的所有點。正如你所看到的值被編碼。我不知道它是什麼編碼,但在谷歌描述了以下文章的算法:

https://developers.google.com/maps/documentation/utilities/polylinealgorithm?csw=1

沒有任何一個有使用在monondorid解碼這個值到List<LatLng>一些代碼?

回答

0

我分享了這個話題,因爲我搜索了很多次以找到我的答案。 Saboor阿萬低於介紹瞭如何用C#進行編碼:

http://www.codeproject.com/Tips/312248/Google-Maps-Direction-API-V3-Polyline-Decoder

這裏是使用在MonoDroid的代碼:

private List<LatLng > DecodePolylinePoints(string encodedPoints) 
{ 
    if (encodedPoints == null || encodedPoints == "") return null; 
    List<LatLng> poly = new List<LatLng>(); 
    char[] polylinechars = encodedPoints.ToCharArray(); 
    int index = 0; 
    int currentLat = 0; 
    int currentLng = 0; 
    int next5bits; 
    int sum; 
    int shifter; 
    try 
    { 
     while (index < polylinechars.Length) 
     { 
      // calculate next latitude 
      sum = 0; 
      shifter = 0; 
      do 
      { 
       next5bits = (int)polylinechars[index++] - 63; 
       sum |= (next5bits & 31) << shifter; 
       shifter += 5; 
      } while (next5bits >= 32 && index < polylinechars.Length); 
       if (index >= polylinechars.Length) 
       break; 
       currentLat += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1); 
       //calculate next longitude 
      sum = 0; 
      shifter = 0; 
      do 
      { 
       next5bits = (int)polylinechars[index++] - 63; 
       sum |= (next5bits & 31) << shifter; 
       shifter += 5; 
      } while (next5bits >= 32 && index < polylinechars.Length); 
       if (index >= polylinechars.Length && next5bits >= 32) 
       break; 
       currentLng += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1); 
      LatLng p = new LatLng(Convert.ToDouble(currentLat)/100000.0, 
       Convert.ToDouble(currentLng)/100000.0); 
      poly.Add(p); 
     } 
    } 
    catch (Exception ex) 
    { 
     //log 
    } 
    return poly; 
} 

只需要與LatLng更換location