0

请参阅以下有关 google 方向 api 响应的参考。

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

如您所见,每个step 标签都包含一个名为polyline. 这个标签包括一个名为points. 据我了解,此标签包括在地图上绘制此方向步骤所需的所有点。如您所见,该值已编码。我不确定它的编码是什么,但在谷歌中描述了以下文章中的算法:

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

有没有人有一些代码可以将这个值解码List<LatLng>为在 monondorid 中使用?

4

1 回答 1

0

我分享了这个主题,因为我搜索了很多次以找到我的答案。以下文章中的 Saboor Awan 描述了如何使用 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;
}

只需要替换locationLatLng.

于 2014-02-09T08:09:01.257 回答