Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google maps API V3 - Can't geocode AutocompleteService predictions

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

like image 877
badsyntax Avatar asked Jan 19 '13 12:01

badsyntax


People also ask

How do I geocode Google Maps API?

Enable Geocoding API In your Google Cloud Platform Console, go to APIs & Services → Dashboard → Enable APIs & Services at the top and choose Maps JavaScript API from the API Library. This will open up the Maps JavaScript API page and Enable it.

Is there a free geocoding API?

positionstack - Free Address Geocoding & Maps API.

Is Google Places Autocomplete free?

The autocomplete request is available at no charge, and the subsequent Place Details call gets charged based on regular Place Details pricing. A Place Details request generates Data SKUs (Basic, Contact, and/or Atmosphere) – depending on the fields that are specified in the request.


2 Answers

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.

like image 92
Dr.Molle Avatar answered Oct 28 '22 05:10

Dr.Molle


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.

var service = new google.maps.places.AutocompleteService();
var request = { input: 'storms river mouth' };
service.getPlacePredictions(request, function (predictions, status) {
    if(status=='OK'){
        geocoder.geocode({ 
            'placeId': predictions[0].place_id
        }, 
        function(responses, status) {
            if (status == 'OK') {
                var lat = responses[0].geometry.location.lat();
                var lng = responses[0].geometry.location.lng();
                console.log(lat, lng);
            }
        });
    }
});
like image 41
Parmjeet Singh Avatar answered Oct 28 '22 04:10

Parmjeet Singh