Android Google Maps Direction Api - Api key restriction not working

前端 未结 3 1335
旧巷少年郎
旧巷少年郎 2020-12-03 22:09

When we are setting a Key restriction to NONE for Google Maps Direction Api, It works fine.

But When we set

3条回答
  •  暖寄归人
    2020-12-03 22:30

    Please try with compile 'com.akexorcist:googledirectionlibrary:1.1.1' flow the doc or try this method And 2nd method Set CameraWithCoordinationBounds for animate Camera:

    private void drawMap(double s_lat,double s_lng,double e_lat,double e_lng) {
            GoogleDirectionConfiguration.getInstance().setLogEnabled(true);
            Log.e("map", "++");
            List 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 stepList = leg.getStepList();
                                    ArrayList 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));
        }
    

提交回复
热议问题