我有一些C#代碼解碼使用谷歌的折線算法編碼的地圖路徑,並試圖將其轉換爲VB.NET。C#到VB.NET轉換(谷歌折線算法解碼器)
這裏的C#代碼,它的工作原理完全:
Collection<Double> decodePolyline(string polyline)
{
if (polyline == null || polyline == "") return null;
char[] polylinechars = polyline.ToCharArray();
int index = 0;
Collection<Double> points = new Collection<Double>();
int currentLat = 0;
int currentLng = 0;
int next5bits;
int sum;
int shifter;
while (index < polylinechars.Length)
{
// calculate next latitude
sum = 0;
shifter = 0;
do
{
next5bits = (int)polylinechars[index++] - 63;
sum |= (next5bits & 31) << shifter;
shifter += 5;
} while (next5bits >= 32 && index < polylinechars.Length);
if (index >= polylinechars.Length)
break;
currentLat += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);
//calculate next longitude
sum = 0;
shifter = 0;
do
{
next5bits = (int)polylinechars[index++] - 63;
sum |= (next5bits & 31) << shifter;
shifter += 5;
} while (next5bits >= 32 && index < polylinechars.Length);
if (index >= polylinechars.Length && next5bits >= 32)
break;
currentLng += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);
points.Add(Convert.ToDouble(currentLat)/100000.0);
points.Add(Convert.ToDouble(currentLng)/100000.0);
}
return points;
}
下面是經度而不是緯度地區代碼 - 工作的VB.NET。
Public Function decodePolyline(ByVal polyline As String) As Collection(Of Double)
If polyline Is Nothing OrElse polyline = "" Then Return Nothing
Dim polylinechars As Char() = polyline.ToCharArray()
Dim points As New Collection(Of Double)
Dim currentLat As Integer = 0
Dim currentLng As Integer = 0
Dim next5bits As Integer
Dim sum As Integer
Dim shifter As Integer
For index As Integer = 0 To polylinechars.Length - 1
'calculate next latitude
sum = 0
shifter = 0
Do
index += 1
next5bits = AscW(polylinechars(index)) - 63
sum = sum Or (next5bits And 31) << shifter
shifter += 5
Loop While next5bits >= 32 AndAlso index < polylinechars.Length
If index >= polylinechars.Length Then
Exit For
End If
currentLat += If((sum And 1) = 1, Not (sum >> 1), (sum >> 1))
'calculate next longitude
sum = 0
shifter = 0
Do
index += 1
next5bits = AscW(polylinechars(index)) - 63
sum = sum Or (next5bits And 31) << shifter
shifter += 5
Loop While next5bits >= 32 AndAlso index < polylinechars.Length
If index >= polylinechars.Length AndAlso next5bits >= 32 Then
Exit For
End If
currentLng += If((sum And 1) = 1, Not (sum >> 1), (sum >> 1))
points.Add(Convert.ToDouble(currentLat)/100000.0)
points.Add(Convert.ToDouble(currentLng)/100000.0)
Next
Return points
End Function
什麼是缺失?
編輯:解決了這個問題(在我下面的答案中糾正的代碼,我不能選擇作爲2天以上的答案)。
(刪除我的回答,因爲我不認爲這是幫助你和我的VB已經墮落爲只讀,所以由我不能修復它只看一眼) – 2011-02-27 09:56:52
它以什麼方式不起作用?它是否編譯?顯示錯誤的結果,與C#版本不同? – tiago2014 2011-02-27 10:01:39
給緯度輸出錯誤。 – amb9800 2011-02-27 10:02:36