首页 文章

在monodroid C#中编码Google API Polyline点数

提问于
浏览
0

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

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

如您所见,每个 step 标记都包含一个名为 polyline 的标记 . 此标记包含名为 points 的子标记 . 据我了解,此标记包含在 Map 上绘制此方向步骤所需的所有点 . 正如您所看到的,该值已被编码 . 我不确定它是什么编码但谷歌在下面的文章中描述了算法:

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

是否有人有一些代码将此值解码为 List<LatLng> 以便在monondorid中使用?

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;
    }
    

    只需要用 LatLng 替换 location .

相关问题