Google Places API types functionality..

前端 未结 5 847
谎友^
谎友^ 2021-01-17 18:56

    
        

        

        
相关标签:
5条回答
  • 2021-01-17 19:29

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

    0 讨论(0)
  • 2021-01-17 19:41

    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"]
    };
    
    0 讨论(0)
  • 2021-01-17 19:41

    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)
    
    0 讨论(0)
  • 2021-01-17 19:45

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

    0 讨论(0)
  • 2021-01-17 19:48

    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.

    0 讨论(0)
提交回复
热议问题