I'm using the google.maps.places.AutocompleteService
to get suggestions for a places search, but I can't geocode some of the predictions.
An example of this: When I search for 'storms river mouth', one of the predictions I get back is 'Storms River Mouth Rest Camp, South Africa', but this address cannot be geocoded to get the lattude/longitude, eg: http://maps.googleapis.com/maps/api/geocode/json?address=Storms%20River%20Mouth%20Rest%20Camp,%20South%20Africa&sensor=true
Is there any way to get lattitude/longitude values for the autocomplete predictions?
Alternatively, I don't understand why google autocomplete is returning predictions that I cannot geocode.
Here is a basic example of the logic and code i'm working with:
var geocoder = new google.maps.Geocoder();
var service = new google.maps.places.AutocompleteService(null, {
types: ['geocode']
});
service.getQueryPredictions({ input: query }, function(predictions, status) {
// Show the predictions in the UI
showInAutoComplete(predictions);
};
// When the user selects an address from the autcomplete list
function onSelectAddress(address) {
geocoder.geocode({ address: address }, function(results, status) {
if (status !== google.maps.GeocoderStatus.OK) {
// This shouldn't never happen, but it does
window.alert('Location was not found.');
}
// Now I can get the location of the address from the results
// eg: results[0].geometry.location
});
}
[edit] - View a working example here: http://demos.badsyntax.co/places-search-bootstrap/example.html
Here is the code I have written that is inspired from @Dr.Molle
Issue I see is a new PlacesService object on every key press that might be an overkill - I don't know the work around although.
Posting it here in-case someone is looking for it.
Maybe this could could help you.
If you need to return all results at once in proper order, use this:
Predictions returned by AutocompleteService has a PlaceId property. You can pass PlaceId instead of address to the geocoder according to documentation https://developers.google.com/maps/documentation/javascript/geocoding.
Use
getPlacePredictions()
instead ofgetQueryPredictions()
. This will return areference
for the place, which you can use to retrieve details by usingplacesService.getDetails()
. The details will contain the geometry for the place.Note: placesService is a google.maps.places.PlacesService-object.
Try this:
Then, the call and the callback:
Sorry for my english. I got a question for you: Can you show me the method showInAutoComplete() ??? I'm showing the predictions on a list of href, but I don't know how save the 'clicked' address value.