Directions API返回0条路线

问题描述

我正在使用Android中的Google Directions API和Retrofit来获取路线。问题是我得到0条路线,但我不知道为什么。这是我拨打电话的代码

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();
                }


            }
        });

服务:

@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);

输出

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: 

我得到200码,但没有找到路线。我在搜索从塞维利亚到马德里的路线。 您看到错误了吗?

提前谢谢!

解决方法

这是因为您请求的格式错误-从您的输出中获取

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=****************

尤其是destination=destination%3D37.389092399999996。应该是destination=37.389092399999996。仔细检查格式化URL的代码,可能会符合以下条件:

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

字符串"destination="是多余的。试试:

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

相反。

更新: 现在似乎是翻新功能。尝试使用类似的东西

...
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.");
}
...