I'm making an app that uses the google maps directions api, which returns me a json, for example:
{
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"place_id" : "ChIJeRLP8gx3GZURImtwaSWGJgY",
"types" : [ "street_address" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJd515ygl3GZURnfYfYs3XfW4",
"types" : [ "street_address" ]
}
],
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : -30.0096913,
"lng" : -51.1507108
},
"southwest" : {
"lat" : -30.0126831,
"lng" : -51.15656300000001
}
},
"copyrights" : "Dados cartográficos ©2017 Google",
"legs" : [
{
"distance" : {
"text" : "0,8 km",
"value" : 847
},
"duration" : {
"text" : "10 minutos",
"value" : 624
},
"end_address" : "Tv. Prof. Emetério García Fernandes, 193 - Cristo Redentor, Porto Alegre - RS, 91370-380, Brasil",
"end_location" : {
"lat" : -30.0126831,
"lng" : -51.1507108
},
"start_address" : "Av. Bernardi, 61 - Cristo Redentor, Porto Alegre - RS, 91040-030, Brasil",
"start_location" : {
"lat" : -30.0096913,
"lng" : -51.15656300000001
},
"steps" : [
{
"distance" : {
"text" : "0,8 km",
"value" : 847
},
"duration" : {
"text" : "10 minutos",
"value" : 624
},
"end_location" : {
"lat" : -30.0126831,
"lng" : -51.1507108
},
"html_instructions" : "Ande para Tv. Prof. Emetério García Fernandes, 193 - Cristo Redentor, Porto Alegre - RS, 91370-380, Brasil",
"polyline" : {
"points" : "pgdvDnofwHR?jBGEcE@cCBiC?[@yA@k@XO~AWt@Kp@Eh@C'AGfBK@sA@sAHAF?DyG"
},
"start_location" : {
"lat" : -30.0096913,
"lng" : -51.15656300000001
},
"steps" : [
{
"distance" : {
"text" : "71 m",
"value" : 71
},
"duration" : {
"text" : "1 min",
"value" : 43
},
"end_location" : {
"lat" : -30.0103252,
"lng" : -51.1565215
},
"html_instructions" : "Siga na direção \u003cb\u003esul\u003c/b\u003e na \u003cb\u003eAv. Bernardi\u003c/b\u003e em direção a \u003cb\u003eAv. Assis Brasil\u003c/b\u003e",
"polyline" : {
"points" : "pgdvDnofwHR?jBG"
},
"start_location" : {
"lat" : -30.0096913,
"lng" : -51.15656300000001
},
"travel_mode" : "WALKING"
},
{
"distance" : {
"text" : "0,3 km",
"value" : 303
},
"duration" : {
"text" : "3 minutos",
"value" : 205
},
"end_location" : {
"lat" : -30.0103521,
"lng" : -51.1533792
},
"html_instructions" : "Vire à \u003cb\u003eesquerda\u003c/b\u003e na \u003cb\u003eAv. Assis Brasil\u003c/b\u003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "pkdvDfofwHEcE@cCBiC?[@yA@k@"
},
"start_location" : {
"lat" : -30.0103252,
"lng" : -51.1565215
},
"travel_mode" : "WALKING"
},
{
"distance" : {
"text" : "0,2 km",
"value" : 247
},
"duration" : {
"text" : "3 minutos",
"value" : 200
},
"end_location" : {
"lat" : -30.0125358,
"lng" : -51.1529738
},
"html_instructions" : "Vire à \u003cb\u003edireita\u003c/b\u003e na \u003cb\u003eAv. do Forte\u003c/b\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "tkdvDr{ewHXO~AWt@Kp@Eh@C'AGfBK"
},
"start_location" : {
"lat" : -30.0103521,
"lng" : -51.1533792
},
"travel_mode" : "WALKING"
},
{
"distance" : {
"text" : "81 m",
"value" : 81
},
"duration" : {
"text" : "1 min",
"value" : 64
},
"end_location" : {
"lat" : -30.012562,
"lng" : -51.1521285
},
"html_instructions" : "Vire à \u003cb\u003eesquerda\u003c/b\u003e na \u003cb\u003ePassagem Dois\u003c/b\u003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "jydvD'yewH@sA@sA"
},
"start_location" : {
"lat" : -30.0125358,
"lng" : -51.1529738
},
"travel_mode" : "WALKING"
},
{
"distance" : {
"text" : "10 m",
"value" : 10
},
"duration" : {
"text" : "1 min",
"value" : 8
},
"end_location" : {
"lat" : -30.01265129999999,
"lng" : -51.1521164
},
"html_instructions" : "Vire à \u003cb\u003edireita\u003c/b\u003e na \u003cb\u003eR. Zéca Neto\u003c/b\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "nydvDxsewHHAF?"
},
"start_location" : {
"lat" : -30.012562,
"lng" : -51.1521285
},
"travel_mode" : "WALKING"
},
{
"distance" : {
"text" : "0,1 km",
"value" : 135
},
"duration" : {
"text" : "2 minutos",
"value" : 104
},
"end_location" : {
"lat" : -30.0126831,
"lng" : -51.1507108
},
"html_instructions" : "Vire à \u003cb\u003eesquerda\u003c/b\u003e na \u003cb\u003eTv. Prof. Emetério García Fernandes\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eO destino estará à direita\u003c/div\u003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "'zdvDvsewHDyG"
},
"start_location" : {
"lat" : -30.01265129999999,
"lng" : -51.1521164
},
"travel_mode" : "WALKING"
}
],
"travel_mode" : "WALKING"
}
],
"traffic_speed_entry" : [],
"via_waypoint" : []
}
],
"overview_polyline" : {
"points" : "pgdvDnofwH~BGEcE@cCBeDBeCXO~AWfBQjBKfBK@sA@sAHAF?DyG"
},
"summary" : "Av. Assis Brasil e Av. do Forte",
"warnings" : [
"Versão beta da rota a pé. Seja cuidadoso – Este trajeto pode não ter calçadas ou caminhos de pedestres."
],
"waypoint_order" : []
}
],
"status" : "OK"
}
I use gson to read this json and use it in my application, one of the uses is this:
public void lerGSON(TransformJSONToGSONGoogleMapsDirections transform) {
if(transform.getStatus().equalsIgnoreCase("OK")) {
distance = transform.getRoutes().get(0).getLegs().get(0).getDistance().getText();
duration = transform.getRoutes().get(0).getLegs().get(0).getDuration().getText();
address = transform.getRoutes().get(0).getLegs().get(0).getEndAddress();
Log.d(TAG, "address: " + address); //Retorna null
ArrayList<Steps> steps = transform.getRoutes().get(0).getLegs().get(0).getSteps();
Log.d(TAG, "travel mode: " + steps.get(0).getTravelMode()); //Retorna null
latLng = new ArrayList<>();
for(int i = 0; i < steps.size(); i++) {
if(steps.get(i).getTravelMode().equalsIgnoreCase("WALKING")) {
String polyline = steps.get(i).getPolyline().getPoints();
for(LatLng p : decodePolyline(polyline)) {
latLng.add(p);
}
}
}
drawRoute(latLng, 255, 63, 81, 181);
setResults();
} else if(transform.getStatus().equalsIgnoreCase("ZERO_RESULTS")) {
criarMsgErro("Não foi possível traçar uma rota para esse destino!");
} else if(transform.getStatus().equalsIgnoreCase("OVER_QUERY_LIMIT")) {
criarMsgErro("Um erro interno ocorreu, por favor tente novamente!");
}
}
I can not get the travel_mode
and the end_address
from json, I checked the path several times and I think it's right
There is no way to put the gson classes because there are many (over 20), but it is in the same json structure
and the value of travel_mode
is not null in json
TransformJSONToGSONGoogleMapsDirections
is the first class of gson, the first object of json and I instantiate it with the name of transform
, thus:
transform.getRoutes().get(0).getLegs().get(0).getEndAddress();
takes the end_address object from legs at position 0, from the routes at position 0
E:
transform.getRoutes().get(0).getLegs().get(0).getSteps().get(0).getTravelMode();
take the travel_mode object from steps at position 0, from legs at position 0, from routes at position 0