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; }