【问题标题】:Encoding Google API Polyline points in monodroid C#在 monodroid C# 中编码 Google API 折线点
【发布时间】:2014-02-09 22:09:22
【问题描述】:

请参阅下面有关 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中使用?

【问题讨论】:

    标签: c# xamarin.android google-maps-android-api-2 google-directions-api


    【解决方案1】:

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

    只需要将location替换为LatLng即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-18
      • 2011-04-20
      • 2011-07-10
      • 2014-05-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多