Note that there are some explanatory texts on larger screens.

plurals
  1. POC# to VB.NET Conversion (Google Polyline Algorithm Decoder)
    primarykey
    data
    text
    <p>I have some C# code that decodes map paths encoded using Google's polyline algorithm and am trying to convert it to VB.NET.</p> <p>Here's the C# code, which works fully:</p> <pre><code>Collection&lt;Double&gt; decodePolyline(string polyline) { if (polyline == null || polyline == "") return null; char[] polylinechars = polyline.ToCharArray(); int index = 0; Collection&lt;Double&gt; points = new Collection&lt;Double&gt;(); int currentLat = 0; int currentLng = 0; int next5bits; int sum; int shifter; while (index &lt; polylinechars.Length) { // calculate next latitude sum = 0; shifter = 0; do { next5bits = (int)polylinechars[index++] - 63; sum |= (next5bits &amp; 31) &lt;&lt; shifter; shifter += 5; } while (next5bits &gt;= 32 &amp;&amp; index &lt; polylinechars.Length); if (index &gt;= polylinechars.Length) break; currentLat += (sum &amp; 1) == 1 ? ~(sum &gt;&gt; 1) : (sum &gt;&gt; 1); //calculate next longitude sum = 0; shifter = 0; do { next5bits = (int)polylinechars[index++] - 63; sum |= (next5bits &amp; 31) &lt;&lt; shifter; shifter += 5; } while (next5bits &gt;= 32 &amp;&amp; index &lt; polylinechars.Length); if (index &gt;= polylinechars.Length &amp;&amp; next5bits &gt;= 32) break; currentLng += (sum &amp; 1) == 1 ? ~(sum &gt;&gt; 1) : (sum &gt;&gt; 1); points.Add(Convert.ToDouble(currentLat) / 100000.0); points.Add(Convert.ToDouble(currentLng) / 100000.0); } return points; } </code></pre> <p>Here's the VB.NET code- works for the longitudes but not latitudes.</p> <pre><code>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) &lt;&lt; shifter shifter += 5 Loop While next5bits &gt;= 32 AndAlso index &lt; polylinechars.Length If index &gt;= polylinechars.Length Then Exit For End If currentLat += If((sum And 1) = 1, Not (sum &gt;&gt; 1), (sum &gt;&gt; 1)) 'calculate next longitude sum = 0 shifter = 0 Do index += 1 next5bits = AscW(polylinechars(index)) - 63 sum = sum Or (next5bits And 31) &lt;&lt; shifter shifter += 5 Loop While next5bits &gt;= 32 AndAlso index &lt; polylinechars.Length If index &gt;= polylinechars.Length AndAlso next5bits &gt;= 32 Then Exit For End If currentLng += If((sum And 1) = 1, Not (sum &gt;&gt; 1), (sum &gt;&gt; 1)) points.Add(Convert.ToDouble(currentLat) / 100000.0) points.Add(Convert.ToDouble(currentLng) / 100000.0) Next Return points End Function </code></pre> <p>What's missing?</p> <p>EDIT: Resolved the issue (corrected code in my answer below, which I can't select as an answer for 2 more days).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload