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

前端 未结 4 1304
暖寄归人
暖寄归人 2020-12-03 08:21

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 de

4条回答
  •  醉梦人生
    2020-12-03 09:08

    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();
        });
    }
    

提交回复
热议问题