Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Places API types functionality..

<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!

like image 924
daveomania_x Avatar asked Nov 18 '11 21:11

daveomania_x


2 Answers

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"]
};
like image 51
streetlogics Avatar answered Oct 20 '22 05:10

streetlogics


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

like image 45
Francois Avatar answered Oct 20 '22 06:10

Francois