7

I cannot figure out how to implement the search box in my google maps. I have it where the user can pick some stuff from a form and it loads the markers on the map. Now I want to add where they can type in the city and state using the google search box, like on maps.google.com. Can this be done with API v. 3?

shinjuo
  • 20,498
  • 23
  • 73
  • 104
  • I don't know how to accomplish with the google search box but there's a couple of examples that could help you to start with your problem [Example of geocoding](http://code.google.com/apis/maps/documentation/javascript/examples/geocoding-simple.html) [Example of geocoding with jquery](http://tech.cibul.net/geocode-with-google-maps-api-v3/) – Jorge Sep 05 '11 at 16:01

1 Answers1

26

There is no complete "widget" in Google maps for this task. But it is easy to accomplish it. In HTML you can have a text field and a button "Search". (Instead you can handle the Enter key in the text field).

<input type="text" id="search_address" value=""/>
<button onclick="search();">Search</button>

In Javascript you instantiate and use a Geocoder:

var addressField = document.getElementById('search_address');
var geocoder = new google.maps.Geocoder();
function search() {
    geocoder.geocode(
        {'address': addressField.value}, 
        function(results, status) { 
            if (status == google.maps.GeocoderStatus.OK) { 
                var loc = results[0].geometry.location;
                // use loc.lat(), loc.lng()
            } 
            else {
                alert("Not found: " + status); 
            } 
        }
    );
};
Jiri Kriz
  • 9,192
  • 3
  • 29
  • 36
  • how can i get additional information like the name or address of that location? – Alp Jan 18 '12 at 12:01
  • 1
    @Alp: the above use case describes the situation where you already know the name or the address and you want to get the corresponding location (lat, lng). If you want to find interesting places in the neighborhood of a location then you might look at [Google places](http://code.google.com/apis/maps/documentation/places/). – Jiri Kriz Jan 23 '12 at 17:33
  • 1
    @Alp I needed an exact thing to what you asked for in your comment and this is what you're looking for: https://developers.google.com/maps/documentation/geocoding/#Geocoding You've probably found what you were looking for by now, but still ;) – mavili Mar 14 '13 at 13:59
  • 1
    what you're looking for is the 'Reverse geocoding' (i.e. looking up textual addresses from latitude and longitude points). – mavili Mar 14 '13 at 14:01