badsyntax badsyntax - 7 months ago 84
Javascript Question

Google maps API V3 - Can't geocode AutocompleteService predictions

I'm using the

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:,%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

// 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:


Use getPlacePredictions() instead of getQueryPredictions() . This will return a reference for the place, which you can use to retrieve details by using placesService.getDetails() . The details will contain the geometry for the place.

Note: placesService is a google.maps.places.PlacesService-object.