How can I make my Google Maps api v3 address search bar work by hitting the enter button on the keyboard?

Posted by Gavin on Stack Overflow See other posts from Stack Overflow or by Gavin
Published on 2012-06-05T04:17:35Z Indexed on 2012/06/05 4:40 UTC
Read the original article Hit count: 169

I'm developing a webpage and I would just like to make something more user friendly. I have a functional Google Maps api v3 and an address search bar. Currently, I have to use the mouse to select search to initialize the geocoding function. How can I make the map return a placemark by hitting the enter button on my keyboard? I just want to make it as user-friendly as possible.

Here is the javascript and div, respectively, I created for the address bar:

var geocoder;
    function initialize() {
    geocoder = new google.maps.Geocoder (); 
    function codeAddress () {
        var address = document.getElementById ("address").value;
        geocoder.geocode ( { 'address': address}, function(results, status)  {
        if (status == google.maps.GeocoderStatus.OK)  {
            map.setCenter(results [0].geometry.location);
            marker.setPosition(results [0].geometry.location);
            map.setZoom(14);
            } 
        else {
            alert("Geocode was not successful for the following reason: " + status);
                }
        }); 
                            }


<div id="geocoder">
        <input id="address" type="textbox" value="">
        <input type="button" value="Search" onclick="codeAddress()">
    </div>

Thank you in advance for your help

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about search