Reputation: 13
I am using Google Directions API and Retrofit in Android to get routes. The problem is that I get 0 routes and I don't know why. This is the code where I make the call:
verRuta.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Geocoder geocoder = new Geocoder(MapsActivity.this, Locale.getDefault());
try {
List<Address> origenAddress = geocoder.getFromLocationName(origen.getText().toString(), 1);
List<Address> destinoAdress = geocoder.getFromLocationName(destino.getText().toString(), 1);
if (origenAddress.size() > 0) {
Double longitude = origenAddress.get(0).getLongitude();
Double latitude = origenAddress.get(0).getLatitude();
origenlatLng[0] = new LatLng(latitude, longitude);
Marker marker = mMap.addMarker(new MarkerOptions().position(origenlatLng[0]));
} else {
Toast.makeText(MapsActivity.this, "No se ha encontrado el origen. Pruebe otro origen.", Toast.LENGTH_LONG).show();
}
if (destinoAdress.size() > 0) {
Double longitude = destinoAdress.get(0).getLongitude();
Double latitude = destinoAdress.get(0).getLatitude();
destinolatLng[0] = new LatLng(latitude, longitude);
Marker marker = mMap.addMarker(new MarkerOptions().position(destinolatLng[0]));
} else {
Toast.makeText(MapsActivity.this, "No se ha encontrado el destino. Pruebe otro destino.", Toast.LENGTH_LONG).show();
}
// Si ambos son correctos, trazamos la ruta y mostramos el botón de comenzar
if (destinoAdress.size() > 0 && origenAddress.size() > 0) {
String cadena_origen = "" + origenAddress.get(0).getLatitude() + "," + origenAddress.get(0).getLongitude();
String cadena_destino = "" + destinoAdress.get(0).getLatitude() + "," + destinoAdress.get(0).getLongitude();
String sensor = "false";
String mode = "driving";
Retrofit retrofit = new Retrofit.Builder().baseUrl(getString(R.string.googleDirectionsURL))
.addConverterFactory(GsonConverterFactory.create())
.build();
Service service = retrofit.create(Service.class);
Call<DirectionResults> call = service.getDireccion(cadena_origen, cadena_destino, sensor, mode, getString(R.string.google_maps_key));
Log.wtf(TAG, ""+call.request().url().toString());
call.enqueue(new Callback<DirectionResults>() {
@Override
public void onResponse(Call<DirectionResults> call, Response<DirectionResults> response) {
if (response.isSuccessful()){
Log.wtf(TAG, "Found routes: "+ new Gson().toJson(response.body()));
//DirectionResults directionResults = response.body();
if(response.body().getRoutes().size() > 0){
Log.wtf(TAG, ""+ response.body().getRoutes().get(0));
}
else{
Log.wtf(TAG, "No se han encontrado rutas.");
}
}
Log.wtf(TAG, "CODE: "+ response.code() + " Message: "+response.message());
}
@Override
public void onFailure(Call<DirectionResults> call, Throwable t) {
Log.d(TAG, t.getMessage());
}
});
Polyline line = mMap.addPolyline(new PolylineOptions()
.add(origenlatLng[0], destinolatLng[0])
.width(5)
.color(Color.RED));
comenzar.setVisibility(View.VISIBLE);
verRuta.setVisibility(GONE);
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
The service:
@GET("json")
Call<DirectionResults> getDireccion(@Query("origin") String origen, @Query("destination") String destino, @Query("sensor") String sensor, @Query("mode") String mode, @Query("key") String key);
And the output:
E/MapsActivity: https://maps.googleapis.com/maps/api/directions/json?origin=36.97177493%2C-5.44197951&destination=40.4167754%2C-3.7037902&sensor=false&mode=driving&key=
W/Looper: Slow Frame: doFrame is 448ms late
E/MapsActivity: Found routes: {"routes":[]}
E/MapsActivity: No se han encontrado rutas.
E/MapsActivity: CODE: 200 Message:
I get a 200 code but no route found. I am searching a route from Sevilla to Madrid. Can you see the error?
Thank you in advance!
Upvotes: 0
Views: 388
Reputation: 13343
This is because you request has wrong format - from your output:
E/MapsActivity: https://maps.googleapis.com/maps/api/directions/json?origin=40.4167754%2C-3.7037902&destination=destination%3D37.389092399999996%2C-5.9844589&sensor=false&mode=driving&key=****************
in particular destination=destination%3D37.389092399999996
. Should be destination=37.389092399999996
. Double check the code that formats URL, probably in line:
String cadena_destino = "destination=" + destinoAdress.get(0).getLatitude() + "," + destinoAdress.get(0).getLongitude();
string "destination="
is redundant. Try:
String cadena_destino = "destinoAdress.get(0).getLatitude() + "," + destinoAdress.get(0).getLongitude();
instead.
Update: Seems now it is Retrofit feature. Try to use something like that
...
Log.wtf(TAG, "Found routes: "+ new Gson().toJson(response.body().bytes()));
if(response.body().getRoutes().size() > 0) {
Log.wtf(TAG, ""+ response.body().getRoutes().get(0));
}
else {
Log.wtf(TAG, "No se han encontrado rutas.");
}
...
Upvotes: 1