我正在编写一个从 XML 文件获取 GPS 坐标并将其发送到 Google Static Maps API 的工具。我正在尝试从 Google 获取折线。
我找到了 documentation到目前为止,我已经写了这个来将 double 值转换为折线的编码值:
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@
这很好,但是如果我使用我的值之一,例如:
LAT: 8.7587061 LONG: 48.6331662
LAT: 8.8905152 LONG: 48.6226701
我得到了错误的值。最终折线应位于德国南部。但是根据我的计算,折线接近成本。也许有一个完成的类(class)给我编码的坐标,但我还没有找到它。
是的,我必须对多段线进行编码,因为 XML 中会有许多不同的坐标(GPS 跟踪器运行几个小时并每 10 秒捕获一次位置)。
最佳答案
好吧,经过又一个晚上的搜索和测试,我找到了解决方案。 Brian Pedersen在他的博客中有一个解决方案。它就像它应该的那样工作。 我不想在此处复制和粘贴代码,但链接有。
关于C# 谷歌静态 map 编码路径计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25693152/