【问题标题】:C# Google Static Maps encoded path calculationC# Google Static Maps 编码路径计算
【发布时间】:2014-09-05 19:59:46
【问题描述】:

我正在编写一个从 XML 文件获取 GPS 坐标并将它们发送到 Google 静态地图 API 的工具。我正在尝试从 Google 获取折线。

我找到了documentation,到目前为止,我已经写了这个来将双精度值转换为折线的编码值:

private String convertDouble(Double _input)
{
    String result = String.Empty;
    //value * 1e5
    Int32 multiplication = Convert.ToInt32(Math.Floor(_input * 1e5));

    //value to binary string
    String binaryString = Convert.ToString(multiplication, 2);

    //binary to hex
    binaryString = BinaryStringToHexString(binaryString);

    //value to hex + 1
    Int32 hexConvert = Convert.ToInt32(binaryString, 16) + Convert.ToInt32("01", 16);

    //value to binary string
    binaryString = Convert.ToString(hexConvert, 2);

    //binary string zu int[] for further calculations
    Int32[] bitValues = new Int32[binaryString.Length];
    for (Int32 i = 0; i < bitValues.Length; i++)
    {
        if (binaryString[i] == '0')
        {
        bitValues[i] = 0;
        }
        else
        {
            bitValues[i] = 1;
        }
    }

    //shift binary to left
    Int32[] bitValues_2 = new Int32[bitValues.Length];
    for (Int32 i = 0; i < bitValues.Length - 1; i++)
    {
        bitValues_2[i] = bitValues[i + 1];
    }
    bitValues_2[bitValues_2.Length - 1] = 0;

    //if input value is negative invert binary
    if (_input < 0)
    {
        for (Int32 i = 0; i < bitValues.Length; i++)
        {
            if (bitValues_2[i] == 0)
            {
            bitValues_2[i] = 1;
                }
            else
            {
                bitValues_2[i] = 0;
            }
        }
    }

    //make blocks of 5
    Int32 lengthDifference = bitValues_2.Length % 5;

    Int32[] bitValues_3 = new Int32[bitValues_2.Length - lengthDifference];

    for (Int32 i = bitValues_2.Length - 1; i > (bitValues_2.Length - bitValues_3.Length); i--)
    {
        bitValues_3[i - (bitValues_2.Length - bitValues_3.Length)] = bitValues_2[i];
    }

    //twist blocks
    Int32[] bitValues_4 = new Int32[bitValues_3.Length];

    Int32 numberOfBlocks = bitValues_3.Length / 5;
    Int32 counter = 0;

    String[] stringValues = new String[numberOfBlocks];

    for (Int32 i = numberOfBlocks - 1; i >= 0; i--)
    {
        for (Int32 j = i * 5; j < (i * 5) + 5; j++)
        {
            bitValues_4[counter] = bitValues_3[j];
            counter++;
        }
    }

    counter = 0;

    //write int[] into strings for final conversion
    for (Int32 i = 0; i < bitValues_4.Length; i++)
    {
        if (i > 0 && i % 5 == 0)
        {
           counter++;
        }

        stringValues[counter] += bitValues_4[i].ToString();
    }

    // blocks ^ 0x20 (32) and convert to char
    Int32 value = 0;
    Int32[] intValues = new Int32[stringValues.Length];
    Char[] charValues = new Char[stringValues.Length];
    for (Int32 i = 0; i < stringValues.Length; i++)
    {
        value = Convert.ToInt32(stringValues[i], 2);
        if (i < stringValues.Length - 1)
        {
            intValues[i] = value ^ 32;
        }
        else
        {
            intValues[i] = value;
        }
            intValues[i] = intValues[i] + 63;
            charValues[i] = (Char)intValues[i];
            result += charValues[i];
        }

    return result;
}

如果我使用来自documentation的值

-179.9832104

我得到结果

`~oia@

这很好,但如果我使用我的价值观之一,例如:

纬度:8.7587061 经度:48.6331662

纬度:8.8905152 经度:48.6226701

我得到了错误的值。最后的折线应该在德国南部。但根据我的计算,折线接近成本。也许有一个完成的类给了我编码的坐标,我还没有找到它。

是的,我必须对折线进行编码,因为 XML 中会有许多不同的坐标(GPS 跟踪器会运行几个小时并每 10 秒捕获一次位置)。

【问题讨论】:

  • 尝试使用decimal 而不是double
  • @Zer0 会有什么变化?
  • @L.B 消除 base 2 浮点问题。
  • @Zer0 有什么问题?您是否阅读过问题和其中的链接?问题与精度无关,与 二进制浮点数十进制浮点数 无关
  • @Zer0 atm 没问题。如果积分有点偏,对客户来说应该没什么大不了的

标签: c# google-maps-api-3 path google-static-maps


【解决方案1】:

好吧,经过又一夜的搜索和测试,我找到了解决方案。 Brian Pedersen 在他的博客中有一个解决方案。它就像它应该的那样工作。 我不想在这里复制粘贴代码,但链接有。

【讨论】:

    猜你喜欢
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多