android - 使用谷歌地图路由

标签 android google-maps routing

   public void get_direction(Activity _activity, GoogleMap _googleMap,
                                 LatLng _origin,LatLng _destination,TextView _txt_distanc,TextView _txt_duration,String mode)
        {
            activity=_activity;
            googleMap=_googleMap;
            origin=_origin;
            destination=_destination;
            txt_distanc=_txt_distanc;
            txt_duration=_txt_duration;

            String o,d,key;
            o=origin.latitude+","+origin.longitude;
            d=destination.latitude+","+destination.longitude;

            key="***";

            final String url = "https://maps.googleapis.com/maps/api/directions/json?origin="+o+"&destination="+d+"&key="+key+"&mode="+mode;
            final StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                               Log.i("onResponse: ",url );

                            Log.i("onResponse: ",response );

                            draw_path(response );
                            get_distans(response);

                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                    Log.i("onResponse: ",url );

                    Log.i("VolleyError: ",error.toString() );

                }
            }
            )
            {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String,String> map =new Hashtable<String,String>();

                    map.put("name", Base64.encodeToString(String.valueOf("").getBytes()
                            , Base64.DEFAULT));

                    return map;
                }
            };

            RequestQueue requestQueue = Volley.newRequestQueue(activity);
            requestQueue.add(stringRequest);
        }

我的代码很少做正确的路由。但是几个小时后,它只是路由,路由预先路由一次,否则会显示以下错误:

{ "error_message" : "This API project is not authorized to use this API.", "routes" : [], "status" : "REQUEST_DENIED" }

最佳答案

您为 url 提供的 API key https://maps.googleapis.com/maps/api/directions/json?origin="+o+"&destination="+d+"&key="+key+"&mode="+mode尚未启用使用 Google 的导航 API。关注此 link启用它。

关于android - 使用谷歌地图路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50097146/

相关文章:

javascript - Google map 适合自定义范围内的标记

routing - 将IP范围转发到在Vagrant/VirtualBox中运行的Kubernetes节点

c# - 当 HTTP-POST 有 body 时,url 参数为 null

java - 无法导入 org.apache.commons.io.output.TeeOutputStream

android - 如何将耗时从现在转换为该日期并将该方法传递给 kotlin 中的 viewholder

Android LinearLayout背景图片

angularjs - Angular + Laravel + UI-Router 问题

Android SQLite 不会向数据库添加值

google-maps - Google Map API V3 事件 mousedown、mousemove 和 mouseup

android - 我如何使用谷歌地图 android api 过滤器搜索餐馆?