编码折线后精度损失

时间:2014-06-19 20:32:01

标签: c# google-maps

在折线编码后,我失去了精度。折线不适合地图。

如何解决精度损失问题?

这是原始折线的10个第一个坐标

enter image description here

Encoded Polyline (download)

要测试输出,我使用此链接:

PolyLineUtility

要对折线进行编码,我使用此C#算法

 public static string Encode(IEnumerable<Coordinate> points)
 {
    var str = new StringBuilder();

    var encodeDiff = (Action<int>)(diff =>
    {
        int shifted = diff << 1;
        if (diff < 0)
            shifted = ~shifted;

        int rem = shifted;

        while (rem >= 0x20)
        {
            str.Append((char)((0x20 | (rem & 0x1f)) + 63));

            rem >>= 5;
        }

        str.Append((char)(rem + 63));
    });

    int lastLat = 0;
    int lastLng = 0;

    foreach (var point in points)
    {
        int lat = (int)Math.Round(point.Latitude * 1E5);
        int lng = (int)Math.Round(point.Longitude * 1E5);

        encodeDiff(lat - lastLat);
        encodeDiff(lng - lastLng);

        lastLat = lat;
        lastLng = lng;
    }

    return str.ToString();
}

0 个答案:

没有答案