9
<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 don't think locality is a type///
    – Jose Vega
    Commented Nov 18, 2011 at 21:14

5 Answers 5

7

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"]
};
5
  • Just went to the link you posted, and while locality is listed in the types, if you look at this link ( code.google.com/apis/maps/documentation/places/… ) , you'll see it only actually lists two (seems weird that they would have a link to a list of types and then right below it say only 2 types work, but that looks like what it is) - so either way, geocode looks like the option you want. Commented Nov 18, 2011 at 21:21
  • Geocode is almost the result I am looking for. Unfortunately, it still lists street names. Ex: If I search for "London, UK", "London Street, SomeCity, SomeCountry" will show up as a result. Where can I find explanations of these types? I don't see anything in Google's docs. Commented Nov 18, 2011 at 21:38
  • Landmarks will return with geocode as well... airports for example. Commented Nov 18, 2011 at 21:41
  • Ah I see now though via your link that only 2 types can be used with the AutoComplete. So I suppose geocode will have to suffice. Thanks for the reply Brad. Commented Nov 18, 2011 at 21:42
  • Daveomania_x, you should accept this as the answer if that's the case. Thanks! Commented Feb 9, 2012 at 22:43
4

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

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

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

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

1
  • The doc say "The exception is that you can safely mix the geocode and establishment types, but note that this will have the same effect as specifying no type" (developers.google.com/places/web-service/…)
    – Dela
    Commented Dec 19, 2019 at 12:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.