开发者

Google direction API json file parsing

开发者 https://www.devze.com 2023-03-15 06:47 出处:网络
I used Google direction API and received data as a json file. My url is like http://maps.googleapis.com/maps/api/directions/json?origin=Adelaide,SA&destination=Adelaide,SA&waypoints=optimiz

I used Google direction API and received data as a json file. My url is like

http://maps.googleapis.com/maps/api/directions/json?origin=Adelaide,SA&destination=Adelaide,SA&waypoints=optimize:true|Barossa+Valley,SA|Clare,SA|Connawarra,SA|McLaren+Vale,SA&sensor=false

and I used optimized:true parameter. As I read it gives me optimal path from source to destination through the waypoints. And now i dont know structure of json file exactly. I l开发者_StackOverflow中文版ook up the structure of json file but i don t know how can i take order of path that Google direction API gives me.


You don't need to understand how to parse it. Use these:

http://developer.android.com/reference/org/json/JSONTokener.html http://developer.android.com/reference/org/json/JSONObject.html http://developer.android.com/reference/org/json/JSONArray.html


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, mDeviceLatlong, point,     GDirectionsApiUtils.MODE_WALKING);
}

/*
 * The callback
 * When the direction 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);
    }
}


The call which you mentioned will give the optimal route that you should follow. In order to know what the route is you need to see the value for waypoint_order.

For your call, the waypoint order is: "waypoint_order" : [ 3, 2, 0, 1 ]

So, the optimal result would be that you need to visit the waypoints in the order: Origin - 3 - 2 - 0 - 1 - Destination.

In your case it would be: Adelaide,SA - McLaren Vale,SA - Connawarra,SA - Barossa Valley,SA - Clare,SA - Adelaide,SA

for your call: http://maps.googleapis.com/maps/api/directions/json?origin=Adelaide,SA&destination=Adelaide,SA&waypoints=optimize:true|Barossa+Valley,SA|Clare,SA|Connawarra,SA|McLaren+Vale,SA&sensor=false

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号