Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you click on a map and have latitude and longitude fields in a form populated?

I have a form that a user needs to fill out where the location needs to be identified. I have latitude and longitude input fields on the form. I am also looking for the decimal lat and long. What I would like seems really simple. I just want to have a link that the user can click on that would popup a map (google or yahoo) and he could use the map to find the location and just click on it and that would automatically populate the two input fields for lat and long from the map. Has anyone done this before?

like image 555
Joe Avatar asked Nov 12 '09 23:11

Joe


1 Answers

Using the Google Maps API, you can do this pretty easily. Just add a click event handler to your map object, which passes in the latitude/longitude coordinates of where the user clicked (see details on the click event here).

function initMap()
{
    // do map object creation and initialization here
    // ...

    // add a click event handler to the map object
    GEvent.addListener(map, "click", function(overlay, latLng)
    {
        // display the lat/lng in your form's lat/lng fields
        document.getElementById("latFld").value = latLng.lat();
        document.getElementById("lngFld").value = latLng.lng();
    });
}
like image 106
Tim S. Van Haren Avatar answered Sep 23 '22 02:09

Tim S. Van Haren