Google Places API types functionality..

£可爱£侵袭症+ 提交于 2019-12-04 03:12:30

问题


<html>
    <head>
        <title></title>

        <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=true"></script>
        <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(function () {
                var input = document.getElementById('location');
                var options = {                    
                    types: ["locality"]
                };

                autocomplete = new google.maps.places.Autocomplete(input, options);
            });
        </script>        
    </head>
    <body>
        <div>Location: <input type="text" id="location" style="width:400px;" /></div>               
    </body>
</html>

There is my code to generate my autocomplete location text input. Google's list of supported types (http://code.google.com/apis/maps/documentation/places/supported_types.html) shows "locality" as being the type to use when I do not wish for everything to come back in the result(businesses, etc). I am getting no results.

Basically, what I would like to achieve is to search for a city (IE: Toronto) And only see results like: "Toronto, ON, Canada". Perhaps I am confused on how I implement this API.

Thank you very much for your replies!


回答1:


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"]
};



回答2:


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




回答3:


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




回答4:


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.




回答5:


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)


来源:https://stackoverflow.com/questions/8189126/google-places-api-types-functionality

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!