开发者

Auto add Lat / lng to form field using google maps

开发者 https://www.devze.com 2023-01-08 06:18 出处:网络
开发者_如何学PythonI\'ve got a html / php form which will add values to a a database. I\'d like to implemented Google maps so that when a user clicks on a location on the map it will automatically add
开发者_如何学Python

I've got a html / php form which will add values to a a database. I'd like to implemented Google maps so that when a user clicks on a location on the map it will automatically add the latitude and longitude of the location which they clicked at to 2 fields on the HTML form (the Latitude and Longitude fields). How would I do this?


It has been quite a while since I last played with google maps, but from what I remember...

First you need an click handler on the map. You then use the point object passed to the click handler, which contains the lng and lat as .x and .y respectively.

GEvent.addListener(myMap, 'click', function(overlay, latLng) {
    if (overlay) {
        // Code for if they clicked an overlay in the map, i.e.- a pin

    } else if (latLng) {
        lngTextbox.value = latLng.lat()
        latTextbox.value = latLng.lng()
    }
}

And yes, this is a duplicate as pointed out above (just noticed that post).

0

精彩评论

暂无评论...
验证码 换一张
取 消