简体   繁体   English

路线 API 返回 0 条路线

[英]Directions API returns 0 routes

I am using Google Directions API and Retrofit in Android to get routes.我在 Android 中使用 Google Directions API 和 Retrofit 来获取路线。 The problem is that I get 0 routes and I don't know why.问题是我得到 0 条路线,我不知道为什么。 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.我得到一个 200 代码,但没有找到路线。 I am searching a route from Sevilla to Madrid.我正在搜索从塞维利亚到马德里的路线。 Can you see the error?你能看到错误吗?

Thank you in advance!先感谢您!

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 .特别是destination=destination%3D37.389092399999996 Should be destination=37.389092399999996 .应该是destination=37.389092399999996 Double check the code that formats URL, probably in line:仔细检查格式化 URL 的代码,可能是一致的:

String cadena_destino = "destination=" + destinoAdress.get(0).getLatitude() + "," + destinoAdress.get(0).getLongitude();

string "destination=" is redundant.字符串"destination="是多余的。 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.");
}
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM