différences entre le décodage PolyUtil JavaScript et Java

Diego Iturriaga:

Je plusieurs points correspondant à un parcours dans le codage PolyUtil. Je montre cette voie dans un Web et dans l'application native Android, le problème est que dans mon web, il est affiché correctement, mais dans mon application android il affiche un peu différent.

Je me suis connecté deux décodages PolyUtil et les points décodés sont différents. L'utilisation PolyUtil site interactif i obtenir la bonne route. Je pense que le problème est dû à la bibliothèque Android.

Le secteur codé PolyUtil:

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

les points android décodés:

-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

points de Web décodés:

-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

dans mon web je décode comme ceci:

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

Aperçu Web

dans l'application android je décode comme ceci:

PolyUtil.decode(encoded_path);

Aperçu Android


Comment puis-je fixer la route Android? Il est montré de manière incorrecte.

Andrii Omelchenko:

Le code comme:

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 polyligne

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

exactement comme dans votre web ( google.maps.geometry.encoding.decodePath(encoded_estimated_wp);) Décodage:

-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
...

décodées polyligne

Vérifiez donc votre android-maps-utilsdans app buld.gradledépendances de fichiers et ajouter dernière la version (0.5+ pour l' instant):

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

ou l' utilisation personnalisée decodePoly()méthode comme dans cette réponse de Marlon ( en fait son attrapé de android-map-utils) et donne le même résultat:

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;
}

Je suppose que tu aimes

Origine http://43.154.161.224:23101/article/api/json?id=189908&siteId=1
conseillé
Classement