2014-09-05 45 views
2

我正在写一个工具,从XML文件获取GPS坐标,并将它们发送到Google Static Maps API。我正在尝试从Google获取折线。C#谷歌静态地图编码路径计算

我发现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.4

我得到的结果

`〜伊亚@

这是好的,但如果我用我的一个值,例如:

纬度:8.7587061 LONG:48.6331662

纬度:8.8905152 LONG:48.6226701

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

是的,我有编码的折线,因为将在XML许多不同的坐标(GPS跟踪几个小时运行,并抓住每10秒的位置)。

+0

尝试使用'decimal'而不是'double'。 – Zer0 2014-09-05 20:34:05

+0

@ Zer0会发生什么变化? – 2014-09-05 20:53:05

+0

@ L.B消除基2浮点问题。 – Zer0 2014-09-05 20:54:03

回答

1

那么,经过一个晚上的搜索和测试,我发现了一个解决方案。 Brian Pedersen在他的博客中有一个解决方案。它的作用就像它应该的一样。 我不想在这里复制和粘贴代码,但链接有。