Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android get and parse Google Directions

google Directions API

I read this guide now I can build a correct request to receive the xml file containg the directions from address A to address B. What I need is some instructions and example on how to read this xml to draw the obtained directions on an Android MapView. I'd like also to know what represents this tag in the xml:

<overview_polyline>
<points>
a~l~Fjk~uOnzh@vlbBtc~@tsE`vnApw{A`dw@~w\|tNtqf@l{Yd_Fblh@rxo@b}
@xxSfytAblk@xxaBeJxlcBb~t@zbh@jc|Bx}C`rv@rw|@rlhA~dVzeo@vrSnc}Axf]fjz@
xfFbw~@dz{A~d{A|zOxbrBbdUvpo@`cFp~xBc`Hk@nurDznmFfwMbwz@bbl@lq~@lo
Ppxq@bw_@v|{CbtY~jGqeMb{iF|n\~mbDzeVh_Wr|Efc\x`Ij{kE}mAb~uF{cNd}xBjp]
fulBiwJpgg@|kHntyArpb@bijCk_Kv~eGyqTj_|@`uV`k|DcsNdwxAott@r}q@_gc@nu`CnvH
x`k@dse@j|p@zpiAp|gEicy@`omFvaErfo@igQxnlApqGze~AsyRzrjAb__@ftyB}pIlo_B
flmA~yQftNboWzoAlzp@mz`@|}_@fda@jakEitAn{fB_a]lexClshBtmqAdmY_hLxiZd~XtaBndgC
</points>
<levels>BBBAAAAABAABAAAAAABBAAABBAAAABBAAABABAAABABBAABAABAAAABABABABBABAABB</levels> 
</overview_polyline> 

thanks

like image 525
urobo Avatar asked Jun 03 '10 10:06

urobo


People also ask

Is Google direction API free?

The Directions API uses a pay-as-you-go pricing model. Directions API requests generate calls to one of two SKUs depending on the type of request: basic or advanced. Along with the overall Google Terms of Use, there are usage limits specific to the Directions API.

What is direction API?

The Directions API is a web service that uses an HTTP request to return JSON or XML-formatted directions between locations. Directions is available in several forms: as a standalone API. as part of the client-side Maps JavaScript API. for server-side use as part of the Client Libraries for Google Maps Web Services.


3 Answers

I found this example on the web I'll try to use it. 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;
}
like image 50
urobo Avatar answered Sep 29 '22 23:09

urobo


I was also trying to use the Direction Api of Google in Android. So I made an open source project to help doing that. You can find it here:https://github.com/MathiasSeguy-Android2EE/GDirectionsApiUtils

How it works, definitly simply:

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);
    }
}
like image 20
Mathias Seguy Android2ee Avatar answered Sep 29 '22 22:09

Mathias Seguy Android2ee


I've tweaked urobo's answer above (very slightly) to give you LatLngs which you'll want for Google Maps for 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;
}
like image 31
Ankhwatcher Avatar answered Sep 29 '22 22:09

Ankhwatcher