How to decode the Google Directions API polylines field into lat long points in Objective-C for iPhone?

后端 未结 13 980
孤街浪徒
孤街浪徒 2020-11-30 19:05

I want to draw routes on a map corresponding to directions JSON which I am getting through the Google Directions API: https://developers.google.com/maps/documentation/direct

13条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 19:45

    In case anyone would need the decoding-code in VBA, here is a (working) port:

        Function decodeGeopoints(encoded)
      decodeGeopoints = ""
      ' This code is a port to VBA from code published here:
      ' http://blog.synyx.de/2010/06/routing-driving-directions-on-android-part-1-get-the-route/
    
      '//decoding
      'List poly = new ArrayList();
    
      '// replace two backslashes by one (some error from the transmission)
      'encoded = encoded.replace("\\", "\");
      encoded = Replace(encoded, "\\", "\")
    
      'int index = 0, len = encoded.length();
      Dim index As Long
      index = 0
      Dim leng As Long
      leng = Len(encoded)
    
      'int lat = 0, lng = 0;
      Dim lat As Long
      lat = 0
      Dim lng As Long
      lng = 0
    
      'while (index < len) {
      While (index < leng)
         'int b, shift = 0, result = 0;
         Dim b, shift, result As Long
         b = 0
         shift = 0
         result = 0
    
         'do {
         Do
            'b = encoded.charAt(index++) - 63;
            index = index + 1
            b = Asc(Mid(encoded, index, 1)) - 63
            'result |= (b & 0x1f) << shift;
            result = result Or ((b And 31) * (2 ^ shift))
    
            'shift += 5;
            shift = shift + 5
         '} while (b >= 0x20);
         Loop While (b >= 32)
         'int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
         Dim dlat As Long
         If (result And 1) <> 0 Then
          dlat = Not Int(result / 2)
         Else
          dlat = Int(result / 2)
         End If
    
         'lat += dlat;
         lat = lat + dlat
    
         'shift = 0;
         shift = 0
         'result = 0;
         result = 0
         'do {
         Do
           'b = encoded.charAt(index++) - 63;
           index = index + 1
           b = Asc(Mid(encoded, index, 1)) - 63
           'result |= (b & 0x1f) << shift;
            result = result Or ((b And 31) * (2 ^ shift))
           'shift += 5;
            shift = shift + 5
         '} while (b >= 0x20);
         Loop While (b >= 32)
         'int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
         Dim dlng As Long
         If (result And 1) <> 0 Then
          dlng = Not Int(result / 2)
         Else
          dlng = Int(result / 2)
         End If
    
         'lng += dlng;
         lng = lng + dlng
    
         'GeoPoint p = new GeoPoint((int) (((double) lat / 1E5) * 1E6), (int) (((double) lng / 1E5) * 1E6));
         Dim myLat, myLng As Double
         myLat = (lat / 100000)
         'myLat = myLat * 1000000
         myLng = (lng / 100000)
         'myLng = myLng * 1000000
    
         'poly.add(p);
         decodeGeopoints = decodeGeopoints & Comma2Dot(myLng) & "," & Comma2Dot(myLat) & ",0 "
      '}
      Wend
    
    End Function
    

提交回复
热议问题