2010-06-03 5 views
12

google Directions APIandroid obtenir et parser Google Directions

J'ai lu ce guide je peux construire une demande correcte pour recevoir le fichier xml containg les directions de l'adresse A à répondre B. Ce que je besoin est des instructions et des exemples sur la façon lire ce xml pour dessiner les directions obtenues sur un MapView Android. Je voudrais aussi savoir ce que représente cette balise dans le xml:

<overview_polyline> 
<points> 
[email protected][email protected]`vnApw{A`[email protected]~w\|[email protected]{[email protected]@b} 
@[email protected][email protected]@jc|Bx}C`[email protected]|@[email protected]}Axf][email protected] 
[email protected]{A~d{A|[email protected]`cFp~xBc`[email protected]@[email protected][email protected] 
[email protected][email protected]|{CbtY~jGqeMb{iF|n\~mbDzeVh_Wr|Efc\x`Ij{kE}mAb~uF{cNd}xBjp] 
[email protected]|[email protected]_Kv~eGyqTj_|@`uV`k|[email protected]}[email protected][email protected]`CnvH 
x`[email protected]@j|[email protected]|[email protected]`[email protected][email protected]}pIlo_B 
[email protected]`@|}[email protected]@jakEitAn{fB_a]lexClshBtmqAdmY_hLxiZd~XtaBndgC 
</points> 
<levels>BBBAAAAABAABAAAAAABBAAABBAAAABBAAABABAAABABBAABAABAAAABABABABBABAABB</levels> 
</overview_polyline> 

grâce

+1

vous pouvez utiliser cette petite bibliothèque de lumière qui parse tout pour vous: https://github.com/perezdidac/google-directions-api –

Répondre

21

J'ai trouvé cet exemple sur le web, je vais essayer de l'utiliser. polyline decoding example

private List<GeoPoint> decodePoly(String encoded) { 

    List<GeoPoint> poly = new ArrayList<GeoPoint>(); 
    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; 

     GeoPoint p = new GeoPoint((int) (((double) lat/1E5) * 1E6), 
      (int) (((double) lng/1E5) * 1E6)); 
     poly.add(p); 
    } 

    return poly; 
} 
0

vous pouvez avoir un aperçu rapide de l'itinéraire qui sera créé par la waypoint_polyline et la liste de coordonnées directement. Pour ce Google ont le décodage version api « Interactive Polyligne Encoder Utility »

Vous pouvez coller la valeur waypoint_polyline au champ texte encodée polyligne à l'adresse Interactive Polyline Encoder Utility

3

je tente aussi d'utiliser l'API de direction de Google dans Android. J'ai donc fait un projet open source pour aider à faire ça. Vous pouvez le trouver ici: https://github.com/MathiasSeguy-Android2EE/GDirectionsApiUtils

Comment ça marche, definitly simplement:

public class MainActivity extends ActionBarActivity implements DCACallBack{ 
/** 
* Get the Google Direction between mDevice location and the touched location using the  Walk 
* @param point 
*/ 
private void getDirections(LatLng point) { 
    GDirectionsApiUtils.getDirection(this, startPoint, endPoint, GDirectionsApiUtils.MODE_WALKING); 
} 

/* 
* The callback 
* When the directions is built from the google server and parsed, this method is called and give you the expected direction 
*/ 
@Override 
public void onDirectionLoaded(List<GDirection> directions) {   
    // Display the direction or use the DirectionsApiUtils 
    for(GDirection direction:directions) { 
     Log.e("MainActivity", "onDirectionLoaded : Draw GDirections Called with path " + directions); 
     GDirectionsApiUtils.drawGDirection(direction, mMap); 
    } 
} 
3

J'ai peaufiné la réponse de urobo ci-dessus (très légèrement) pour vous donner LatLng que vous aurez envie de Google Maps pour V2 Android:

private List<LatLng> decodePoly(String encoded) { 

    List<LatLng> poly = new ArrayList<LatLng>(); 
    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; 
} 
Questions connexes