Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Places v3 API autocomplete - cannot get locality working?

I had been using the following code, which was working fine for a week or so, i've come back to work on it further and it no longer seems to work. I am wanting to show localities only. Looking at Google's documentation "locality" is the correct option to do so. I was thinking that perhaps google changed the API or something. As soon as i switch it to geocode it works but shows all streets and places, which is not what I want. I also tried ["locality","political"] as well with no luck. What am I doing wrong here?

<script type="text/javascript">
  function initialize() {

    var input = document.getElementById('searchTextField');

    var Newoptions = {
      types: ["locality"]
    }; 

    var autocomplete = new google.maps.places.Autocomplete(input,Newoptions);

  }
  google.maps.event.addDomListener(window, 'load', initialize);
</script>

<input id="searchTextField" name="member_location" value="" type="text" >
like image 870
shwebdev Avatar asked Nov 14 '22 15:11

shwebdev


1 Answers

Locality is not always provided in the results, so you have to provide a fallback if you're in an area with no locality, like administrative_area_level_3, though the only one you will always get is country, so you'd better have it fallback eventually to there.

like image 72
user1131682 Avatar answered Nov 17 '22 06:11

user1131682