Android Google Maps Direction Api - Api key restriction not working
S

3

12

enter image description here

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

But When we set Key restriction to Android apps and provide a proper Package name & SHA-1 certificate - It says Request Declined from Google Api response.

Any known solution to this?

Stylite answered 12/9, 2017 at 13:51 Comment(2)
in debug or live?Colorless
for both it's not working.Stylite
O
9

Directions API is a web service. The restrictions that will work with an API keys for web services are IP restrictions.

It is supposed that web services requests are executed on your backend servers. If you need to restrict an API key, the workaround is to create an intermediate server. Your Android application should send requests to the intermediate server, intermediate server should send requests to Google and pass responses back to your app. In this case you can restrict an API key by IP address of your intermediate server.

Have a look at this document:

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

Hope this clarifies your doubt.

Oiler answered 12/9, 2017 at 21:10 Comment(3)
So What I conclude is that without intermediate server Any one with a Api key can make a request and get successful response of Direction Api. But what about the SHA-1 finger print they are asking for then? There should be ony 2 options : A) NONE B) Server IP addressStylite
Android app restriction with SHA1 is used for Google Maps Android API only, the IP restriction is used for web services (Geocoding API, Directions API, etc.). In order to protect an API key used by web service you must send requests from the server that you own and use its IP as a restriction.Oiler
I am finding it strange that Google Map Direction Api key can't be restricted by Android App itself by providing unique fingerprint. But so far it's the best answer I can have. So I am thankful to you & accepting your answer.Stylite
S
0

You will usually have more than one certificate. One for debug and one for release.

Ensure that you add both fingerprints or that the certificate fingerprint you are using matches the one for the buildType you specified

Sangraal answered 12/9, 2017 at 13:55 Comment(1)
I did that buddy. I have tried with only debug - but after unsuccessful effort I did go with release. But failed for both.Stylite
I
-1

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<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));
    }
Idalia answered 24/1, 2018 at 12:17 Comment(1)
Can you explain from your answer that How is it suppose to restrict the key?Stylite

© 2022 - 2024 — McMap. All rights reserved.