How to add draggable markers to a Google Map using jQuery

前端 未结 1 478
小鲜肉
小鲜肉 2021-02-05 21:50

I\'m playing with google Maps and I need some directions :-)

I\'d like a map with a draggable marker. If the user drags the marker some formfields need to be updated wit

1条回答
  •  北荒
    北荒 (楼主)
    2021-02-05 22:37

    You dont need jquery, it won't make it any easier.

    First create your map and add a draggable marker.

    var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
    var marker = new google.maps.Marker({
        position: myLatlng, 
        map: map, // handle of the map 
        draggable:true
    });
    

    Then you just need to add an event listener to the map that listens for the marker drag event and updates the textboxes.

    google.maps.event.addListener(
        marker,
        'drag',
        function() {
            document.getElementById('lat').value = marker.position.lat();
            document.getElementById('lng').value = marker.position.lng();
        }
    );
    

    http://jsfiddle.net/xTh5U/

    0 讨论(0)
提交回复
热议问题