2010-06-03 10 views
12

google Directions APIandroid uzyskać i zanalizować Google Kierunki

czytam ten poradnik teraz mogę zbudować poprawny wniosek do otrzymania pliku xml zawierajacego kierunki od skierować do zajęcia B. Co potrzebne jest pewne instrukcje i przykład, w jaki sposób przeczytać ten xml, aby narysować otrzymane wskazówki na Android MapView. Chciałbym również wiedzieć, co reprezentuje ten tag w 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> 

dzięki

+1

Możesz użyć tej małej biblioteki światła, która analizuje wszystko dla Ciebie: https://github.com/perezdidac/google-directions-api –

Odpowiedz

21

Znalazłem ten przykład na stronie postaram się go używać. 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

może chcesz mieć szybki przegląd trasy, który zostanie utworzony przez waypoint_polyline oraz wykazu współrzędnych bezpośrednio. W tym google mieć zwalniającą dekodowania API „Interaktywny Łamana Encoder Utility”

Możesz wkleić wartość waypoint_polyline do pola tekstowego kodowane polilini adres Interactive Polyline Encoder Utility

3

Ja również próbuje użyć API kierunku Google w Androidzie. Więc zrobiłem projekt open source, aby pomóc w tym. Można go znaleźć tutaj: https://github.com/MathiasSeguy-Android2EE/GDirectionsApiUtils

Jak to działa, zdecydowanie prościej:

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

mam manipulowane odpowiedź urobo za wyżej (bardzo nieznacznie), aby dać Ci LatLngs której chcesz na Mapach Google dla Android V2:

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; 
} 
Powiązane problemy