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秒的位置)。
嘗試使用'decimal'而不是'double'。 – Zer0 2014-09-05 20:34:05
@ Zer0會發生什麼變化? – 2014-09-05 20:53:05
@ L.B消除基2浮點問題。 – Zer0 2014-09-05 20:54:03