4

I have multiple points corresponding to a route in PolyUtil encoding. I show this route in a web and in native android app, the issue is that in my web it is shown correctly but in my android app it displays a little different.

I logged both PolyUtil decodings and the points decoded are different. Using PolyUtil interactive site i get the correct route. I think the issue is with android library.

PolyUtil encoded points:

lvzsEhgvuI~c@HbJr@lJ`@|MrDfJlE~GxCdEvQ~IhCfDh@bIi@dGwB|D{GL{VqBqGcHcEeNGqGpGmHnQ

android points decoded:

-34.88199, -56.15223
-34.883770000000005, -56.152280000000005
-34.885600000000004, -56.15254
-34.88799, -56.152710000000006
-34.889790000000005, -56.15361000000001
-34.89123, -56.15464000000001
-34.89222, -56.15541
-34.893980000000006, -56.15841
-34.89482, -56.1591
-34.896440000000005, -56.159310000000005
-34.89775, -56.1591
-34.898700000000005, -56.158500000000004
-34.898770000000006, -56.15708000000001
-34.8982, -56.15326
-34.89674, -56.15189
-34.894310000000004, -56.15091
-34.89294, -56.150870000000005
-34.89143, -56.152240000000006
-34.891290000000005, -56.15520000000001

web points decoded:

-34.876070000000006,-56.152370000000005
-34.88199,-56.152420000000006
-34.883770000000005,-56.152680000000004
-34.885600000000004,-56.15285000000001
-34.88799,-56.15375
-34.889790000000005,-56.15478
-34.89123,-56.155550000000005
-34.89222,-56.158550000000005
-34.893980000000006,-56.159240000000004
-34.89482,-56.15945000000001
-34.896440000000005,-56.159240000000004
-34.89775,-56.158640000000005
-34.898700000000005,-56.15722
-34.898770000000006,-56.153400000000005
-34.8982,-56.15203
-34.89674,-56.151050000000005
-34.894310000000004,-56.15101000000001
-34.89294,-56.15238000000001
-34.89143,-56.15534

in my web i decode like this:

google.maps.geometry.encoding.decodePath(encoded_estimated_wp);

Web preview

in android app i decode like this:

PolyUtil.decode(encoded_path);

Android preview


How can i fix the android route? It is shown incorrectly.

  • 1
    One clue for me is the first point in the web decode list is not present in the android decode list (ignoring the fractional differences for the moment) and the last point in the android decode list is not present in the web decode list. And both have the same number of points. This is hard to reconcile unless one suspects the string. How does the `encoded_path` in your android code get populated and post the code which logs the points. –  Jan 25 '19 at 18:27

1 Answers1

2

The code like:

List <LatLng> pathPoints = PolyUtil.decode("lvzsEhgvuI~c@HbJr@lJ`@|MrDfJlE~GxCdEvQ~IhCfDh@bIi@dGwB|D{GL{VqBqGcHcEeNGqGpGmHnQ");
for (LatLng point : pathPoints) {
    Log.d(TAG, "" + point.latitude + "," + point.longitude);
}
PolylineOptions polylineOptions = new PolylineOptions()
        .addAll(pathPoints)
        .color(Color.RED)
        .width(20);

decode polyline

lvzsEhgvuI~c@HbJr@lJ`@|MrDfJlE~GxCdEvQ~IhCfDh@bIi@dGwB|D{GL{VqBqGcHcEeNGqGpGmHnQ

exactly as in your web (google.maps.geometry.encoding.decodePath(encoded_estimated_wp);) decode:

-34.876070000000006,-56.152370000000005
-34.88199,-56.152420000000006
-34.883770000000005,-56.152680000000004
-34.885600000000004,-56.15285000000001
-34.88799,-56.15375
-34.889790000000005,-56.15478
-34.89123,-56.155550000000005
-34.89222,-56.158550000000005
-34.893980000000006,-56.159240000000004
...

Decoded polyline

So check your android-maps-utils in app buld.gradle file dependences and add latest version (0.5+ for now):

dependencies {
    ...
    implementation 'com.google.maps.android:android-maps-utils:0.5+'
}

or use custom decodePoly() method like in this answer of Marlon (actually its grabbed from android-map-utils) and gives the same result:

private List<LatLng> decodePoly(String encoded) {

  List<LatLng> poly = new ArrayList<>();
  int index = 0, len = encoded.length();
  int lat = 0, lng = 0;

  while (index < len) {
      int b, shift = 0, result = 0;
      do {
          b = encoded.charAt(index++) - 63;
          result |= (b & 0x1f) << shift;
          shift += 5;
      } while (b >= 0x20);
      int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
      lat += dlat;

      shift = 0;
      result = 0;
      do {
          b = encoded.charAt(index++) - 63;
          result |= (b & 0x1f) << shift;
          shift += 5;
      } while (b >= 0x20);
      int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
      lng += dlng;

      LatLng p = new LatLng((((double) lat / 1E5)),
              (((double) lng / 1E5)));
      poly.add(p);
  }

  return poly;
}
Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
  • 2
    Thank you Andrii, your answer guided me to the solution. The issue was that the json object obj.get("coordenadas").toString() adds some quotes to the string at the beginning and at the end and gave me this strange behaviour. I replaced it with obj.get("coordenadas").getAsString() and everything is fine now. – Diego Iturriaga Jan 30 '19 at 13:22