Android Google Maps Direction Api - Api密钥限制不起作用

时间:2017-09-12 13:51:02

标签: android google-maps google-maps-android-api-2 google-maps-api-2 google-maps-direction-api

enter image description here

当我们为Google Maps Direction Api设置密钥限制 时,它运行正常。

但是,当我们将密钥限制设置为 Android应用 并提供正确的套餐名称& SHA-1证书 - 它表示请求已从Google Api响应中拒绝。

任何已知的解决方案?

3 个答案:

答案 0 :(得分:5)

Directions API是一项网络服务。与Web服务的API密钥一起使用的限制是IP限制。

假设您的后端服务器上执行了Web服务请求。如果需要限制API密钥,则解决方法是创建中间服务器。您的Android应用程序应该向中间服务器发送请求,中间服务器应该向Google发送请求并将响应传递回您的应用程序。在这种情况下,您可以通过中间服务器的IP地址限制API密钥。

看一下这个文件:

https://developers.google.com/maps/faq#using-google-maps-apis

希望这能澄清你的怀疑。

答案 1 :(得分:0)

您通常会拥有多个证书。一个用于调试,一个用于发布。

确保添加两个指纹或您使用的证书指纹与您指定的buildType的指纹匹配

答案 2 :(得分:0)

请尝试  编译'com.akexorcist:googledirectionlibrary:1.1.1' 流动文档或尝试此方法 第二种方法设置CameraWithCoordinationBounds来设置相机动画:

private void drawMap(double s_lat,double s_lng,double e_lat,double e_lng) {
        GoogleDirectionConfiguration.getInstance().setLogEnabled(true);
        Log.e("map", "++");
        List<LatLng> waypoints = Arrays.asList(

                new LatLng(22.626390800000003, 88.4313014), new LatLng(22.619708499999998, 88.4369083)
        );
        GoogleDirection.withServerKey("AIz... your google api key")
                .from(new LatLng(s_lat, s_lng))
                .and(waypoints)
                .to(new LatLng(e_lat, e_lng))
                .transportMode(TransportMode.DRIVING)
                .execute(new DirectionCallback() {
                    @Override
                    public void onDirectionSuccess(Direction direction, String rawBody) {
                        if (direction.isOK()) {
                            mMap.setMinZoomPreference(8f);
                            com.akexorcist.googledirection.model.Route route = direction.getRouteList().get(0);
                            int legCount = route.getLegList().size();
                            for (int index = 0; index < legCount; index++) {
                                Log.e("map", "++++" + index);
                                Leg leg = route.getLegList().get(index);
                                // mMap.addMarker(new MarkerOptions().position(leg.getStartLocation().getCoordination()));

                                if (index == 0) {
                                    Log.e("position","0" + leg.getStartLocation().getCoordination());
                                    //   mMap.addMarker(new MarkerOptions().position(leg.getEndLocation().getCoordination()).title("User"));
                                    mMap.addMarker(new MarkerOptions().position(leg.getStartLocation().getCoordination()).icon(BitmapDescriptorFactory
                                            .fromResource(R.drawable.start_pointer)));
                                } else if (index == legCount - 1) {
                                    //   mMap.addMarker(new MarkerOptions().position(leg.getEndLocation().getCoordination()).title("User"));
                                    mMap.addMarker(new MarkerOptions().position(leg.getEndLocation().getCoordination()).icon(BitmapDescriptorFactory
                                            .fromResource(R.drawable.stop_pointer)));
                                } else {
                                    mMap.addMarker(new MarkerOptions().position(leg.getEndLocation().getCoordination()).icon(BitmapDescriptorFactory
                                            .fromResource(R.drawable.user_point)));
                                }
                                List<Step> stepList = leg.getStepList();
                                ArrayList<PolylineOptions> polylineOptionList = DirectionConverter.createTransitPolyline(MainActivity.this, stepList, 5, Color.RED, 3, Color.BLUE);
                                for (PolylineOptions polylineOption : polylineOptionList) {
                                    mMap.addPolyline(polylineOption);
                                }
                            }
                            setCameraWithCoordinationBounds(route); // animateCamera

                        }
                    }

                    @Override
                    public void onDirectionFailure(Throwable t) {

                        Log.e("error", t.getLocalizedMessage() + t.getMessage() + "");
                        // Do something
                    }
                });
    }

  private void setCameraWithCoordinationBounds(com.akexorcist.googledirection.model.Route route) {
        LatLng southwest = route.getBound().getSouthwestCoordination().getCoordination();
        LatLng northeast = route.getBound().getNortheastCoordination().getCoordination();
        LatLngBounds bounds = new LatLngBounds(southwest, northeast);
        mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100));
    }
相关问题