Google Maps API 3 Search Box

I cannot figure out how to implement a search box on my google maps. I have it, where the user can select some things from the form and upload markers to the map. Now I want to add where they can enter the city and state using the google search box, for example, on maps.google.com. This can be done using API v. 3?

+7
source share
1 answer

Google maps does not have a complete widget for this. But it is easy to implement. In HTML, you can have a text box and a search button. (Instead, you can handle the Enter key in the text box).

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

In Javascript, you instantiate and use the 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); } } ); }; 
+26
source

All Articles