1

I need to build a direction in my android application so 1- i get new key from google api console 2- i did the billing and got free trial 3- i follow the instruction in google developers but i have a problem Invalid Api key i note that there is a missing part in my work which is step two in google guide Step 2: Add the API key to your app When loading the Directions API, substitute YOUR_API_KEY in the code below with the API key you got from the previous step https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&key=YOUR_API_KEY but i don't know where to put this code ? and finally this is my all code :

    DateTime now = new DateTime();
        try {
            DirectionsResult result = DirectionsApi.newRequest(getGeoContext())
                    .mode(TravelMode.DRIVING).origin(String.valueOf(userLocation))
                    .destination(String.valueOf(sydney)).departureTime(now)
                    .await();
            addMarkersToMap(result,mMap);
            addPolyline(result,mMap);


        } catch (ApiException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
  private GeoApiContext getGeoContext() {
    GeoApiContext geoApiContext = new GeoApiContext();
    return geoApiContext.setQueryRateLimit(3)
            .setApiKey("@string/google_maps_key")
            .setConnectTimeout(1, TimeUnit.SECONDS)
            .setReadTimeout(1, TimeUnit.SECONDS)
            .setWriteTimeout(1, TimeUnit.SECONDS);
}
private void addMarkersToMap(DirectionsResult results, GoogleMap mMap) {
    mMap.addMarker(new MarkerOptions().position(new LatLng(results.routes[0].legs[0].startLocation.lat,results.routes[0].legs[0].startLocation.lng)).title(results.routes[0].legs[0].startAddress));
    mMap.addMarker(new MarkerOptions().position(new LatLng(results.routes[0].legs[0].endLocation.lat,results.routes[0].legs[0].endLocation.lng)).title(results.routes[0].legs[0].startAddress)
            .snippet(getEndLocationTitle(results)));
}
private void addPolyline(DirectionsResult results, GoogleMap mMap) {
    List<LatLng> decodedPath = PolyUtil.decode(results.routes[0].overviewPolyline.getEncodedPath());
    mMap.addPolyline(new PolylineOptions().addAll(decodedPath));
}
private String getEndLocationTitle(DirectionsResult results){
    return  "Time :"+ results.routes[0].legs[0].duration.humanReadable + " Distance :" + results.routes[0].legs[0].distance.humanReadable;    }
it city
  • 11
  • 4

2 Answers2

0

You are messing the Javascript API of Google Directions with the Map SDK for Android that you're using.

Check out the doc here: https://developers.google.com/maps/documentation/android-sdk/signup

In AndroidManifest.xml, add the following element as a child of the element, by inserting it just before the closing tag:

<meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="YOUR_API_KEY"/>
Laurent Meyer
  • 2,766
  • 3
  • 33
  • 57
  • thank you , but i already added the meta data in manifist what about this step Add the API key to your app When loading the Directions API, substitute YOUR_API_KEY in the code below with the API key you got from the previous step https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&key=YOUR_API_KEY – it city Oct 04 '18 at 19:20
  • please can you help – it city Oct 05 '18 at 11:26
  • @itcity Did you grant the access to your app with the SHA1 Process? – Laurent Meyer Oct 05 '18 at 12:41
  • .setApiKey("@string/google_maps_key") this was my mistake – it city Oct 05 '18 at 13:22
0

[SOLVED] this was the reason

            .setApiKey("@string/google_maps_key")

it take this as string not the google api key so t

it city
  • 11
  • 4