Reputation: 741
According to the maps optimisations guide it is possible to reduce usage of Google Places by using the GeoCoding API to retrieve a place location by place Id.
Geocoding API
If your application handles user-typed addresses, the addresses are sometimes ambiguous (incomplete, misspelled, or poorly formatted). You can disambiguate addresses using Autocomplete. Then, use the place IDs to get the place locations.
Mobile
Maps
For mobile applications, use Maps SDK for Android or Maps SDK for iOS when displaying a map. The mobile SDKs are free of charge and have unlimited quota. Use Maps Static API or Maps JavaScript API when requirements rule out using the mobile SDKs.
Is such optimisation possible using Android?
The Geocoder in Android Location package doesn't expose any method to retrieve an address from a placeId only from lat,long or name:
List<Address> getFromLocation(double latitude, double longitude, int maxResults);
List<Address> getFromLocationName(String locationName, int maxResults);
List<Address> getFromLocationName(String locationName, int maxResults, double lowerLeftLatitude, double lowerLeftLongitude, double upperRightLatitude, double upperRightLongitude);
Javascript Code:
// This function is called when the user clicks the UI button requesting
// a geocode of a place ID.
function geocodePlaceId(geocoder, map, infowindow) {
var placeId = document.getElementById('place-id').value;
geocoder.geocode({'placeId': placeId}, function(results, status) {
if (status === 'OK') {
if (results[0]) {
map.setZoom(11);
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
infowindow.setContent(results[0].formatted_address);
infowindow.open(map, marker);
} else {
window.alert('No results found');
}
} else {
window.alert('Geocoder failed due to: ' + status);
}
});
Upvotes: 0
Views: 1332
Reputation: 32138
The native Android API geocoder doesn't support getting addresses by place ID. Unfortunately, Google Maps Android SDK doesn't provide built-in Geocoder neither. The feature request exists for a long time, but it looks like it doesn't have high priority:
https://issuetracker.google.com/issues/35823852
So, to use place ID in requests you are stick to REST API. There is a Java client library for Google Maps API Web Services that you can find on github:
https://github.com/googlemaps/google-maps-services-java
You can use this library to call Geocoding API from your Java code in Android.
GeoApiContext context = new GeoApiContext.Builder()
.apiKey("AIza...")
.build();
GeocodingResult[] results = GeocodingApi.newRequest(context)
.place("ChIJHzwQtJeLGGARxaSLI71pDSY").await();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(results[0].addressComponents));
Note that API key for web services must be different from an API key that you used in Android app, because web services don't support Android app restriction.
I hope this helps!
Upvotes: 1