Google Places API types functionality..

丶灬走出姿态 提交于 2019-12-01 16:42:38

I think the option you are looking for according to the docs is "geocode" ( http://code.google.com/apis/maps/documentation/javascript/reference.html#AutocompleteOptions ):

var options = {                    
    types: ["geocode"]
};

you can also use the country restriction

example:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script>

<script type="text/javascript">
    function initialize() 
    {
      var input = document.getElementById('searchTextField');
      var options = {
          types: ['(cities)'],
          componentRestrictions: {country: "ca"}
      };

      autocomplete = new google.maps.places.Autocomplete(input, options);
    }

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

<input id="searchTextField" type="text" size="50" placeholder="Anything you want!">

now you can easily add a dropdown with a selection of cities and re-filter the cities, when onchange of the dropdown occurs :)

You can use like this types: ['geocode' || 'establishment' || 'address']

Try, check out the jsfiddle:

 var options = {                    
      types: ['geocode']
 };

types, which can be either establishment or geocode, representing businesses or addresses, respectively. If types is not specified, both types are returned.

If the user types Tor in the input field and the output you want is Toronto, ON, Canada then you should use types=(regions), with the brackets.

I don't know if the option was present when the question was asked, but it is available now.

Sample Request:

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Tor&key=<YOUR_API_KEY_HERE>&types=(regions)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!