Retrieve latitude and longitude of a draggable pin via Google Maps API V3

后端 未结 8 529
礼貌的吻别
礼貌的吻别 2020-11-29 18:23

I will explain. I managed to have a draggable pin on a map. I want to retrieve the coordinates of this point and put them into two fields: Latitude and Longitude. These coor

8条回答
  •  臣服心动
    2020-11-29 19:20

    Either of these work

    google.maps.event.addListener(marker, 'click', function (event) {
        document.getElementById("latbox").value = event.latLng.lat();
        document.getElementById("lngbox").value = event.latLng.lng();
    });
    
    google.maps.event.addListener(marker, 'click', function (event) {
        document.getElementById("latbox").value = this.getPosition().lat();
        document.getElementById("lngbox").value = this.getPosition().lng();
    });
    

    You might also consider using the dragend event also

    google.maps.event.addListener(marker, 'dragend', function (event) {
        document.getElementById("latbox").value = this.getPosition().lat();
        document.getElementById("lngbox").value = this.getPosition().lng();
    });
    

提交回复
热议问题