2010-06-03 7 views
12

google Directions APIandroide obtener y analizar sintácticamente Google llegar

leí esta guía ahora puedo construir una solicitud correcta para recibir el archivo XML containg las direcciones desde la dirección A para hacer frente a B. Lo que necesito es un poco de instrucciones y ejemplo sobre cómo para leer este xml para dibujar las direcciones obtenidas en un Android MapView. Me gustaría también saber qué representa esta etiqueta en el 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> 

gracias

+1

Es posible que desee utilizar esta biblioteca pequeña luz que analiza todo para usted: https://github.com/perezdidac/google-directions-api –

Respuesta

21

me encontré con este ejemplo en la web Voy a tratar de usarlo. 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

es posible que desee tener una visión rápida de la ruta que será creado por el waypoint_polyline y la lista de coordenadas directamente. Para ello Google han comunicado de decodificación api "interactivo polilínea Encoder Utility"

Puede pegar el valor waypoint_polyline al campo de texto codificado polilínea en la dirección Interactive Polyline Encoder Utility

3

también estaba tratando de utilizar la API de Dirección de Google en Android. Así que hice un proyecto de código abierto para ayudar a hacer eso. Se puede encontrar aquí: https://github.com/MathiasSeguy-Android2EE/GDirectionsApiUtils

Cómo funciona, definitivamente simplemente:

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

he pellizcado respuesta de urobo anterior (muy ligeramente) para darle LatLng que usted desee para Google Maps para 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; 
} 
Cuestiones relacionadas