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; }
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"/>
[SOLVED] this was the reason
.setApiKey("@string/google_maps_key")
it take this as string not the google api key so t
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.