2

I'm trying to use Google Maps Autocomplete API but there is some discrepancies between it and Geocoder API, for example:

On the autocomplete API, I'm trying to search the address "Rua Tiradentes, 1020, Ibirubá", when I select on the list, the address component "street_number" doesn't come, it's an issue with this and other addresses.

Let's try with geocoder API:

var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': 'Rua Tiradentes, 1020, Ibirubá' }, function (results, status) {
   if (status == google.maps.GeocoderStatus.OK) {
      console.log(results);
   } else {
      alert("Geocoder failed due to: " + status);
   }
});

Now, the street_number is inside the address_components, is there any workaround or Google Autocomplete isn't reliable?

Here is the places code:

var b = new google.maps.places.Autocomplete(myInput);

google.maps.event.addListener(b, 'place_changed', function () {
    console.log(b.getPlace());
})
Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
Alexandre
  • 7,004
  • 5
  • 54
  • 72
  • 1
    The documentation is not very clear: *The following types **may be returned** in the results of a place search, in addition to the types in table 1 above.* See https://developers.google.com/places/documentation/supported_types#table2 So if it *may be returned*, we don't really know when and why it will/will not be returned. – MrUpsidown Oct 29 '14 at 16:19
  • And basically, the geocoding documentation is quite vague too: *In addition to the above, address components **may include** the types below.* See: https://developers.google.com/maps/documentation/geocoding/#Types – MrUpsidown Oct 29 '14 at 16:25
  • I reported a bug on maps API, it should behave like Geocoder API. I wrote a fallback, if street_number is null, get the input text and make a geocoding request – Alexandre Oct 29 '14 at 16:54
  • It will most probably not be acknowledged as a bug, since the doc doesn't say it **will** return it, but it's still worth a try ;) Please share the link to the bug report! – MrUpsidown Oct 30 '14 at 00:01
  • 1
    Here is it: https://code.google.com/p/gmaps-api-issues/issues/detail?id=7299&q=apitype%3APlacesAPI%20type%3ADefect&sort=-stars&colspec=ID%20Type%20Status%20Introduced%20Fixed%20Summary%20Internal%20Stars – Alexandre Oct 30 '14 at 13:33

1 Answers1

0

The possible workaround is the following. You can try to geocode the place to get all address components. Recently, Google added possibility to geocode addresses using the placeId as a request parameter.

You can find more detailed information in the documentation: https://developers.google.com/maps/documentation/javascript/geocoding https://developers.google.com/maps/documentation/javascript/3.exp/reference#GeocoderRequest

So, you can get the place like var place = autocomplete.getPlace(); you can obtain the place ID like place.place_id and execute geocoding request like this one:

geocoder.geocode( { 'placeId': place.place_id}, function(results, status) { 
  if (status == google.maps.GeocoderStatus.OK) { 
    console.log(results); 
  } else { 
    alert('Geocode was not successful for the following reason: ' + status); 
  } 
}); 
xomena
  • 31,125
  • 6
  • 88
  • 117