Add marker to Google Map on Click

前端 未结 6 1929
有刺的猬
有刺的猬 2020-12-12 09:50

I\'m surprisingly struggling to find a very simple example of how to add a marker(s) to a Google Map when a user left clicks on the map.

I have looked around for the

6条回答
  •  情深已故
    2020-12-12 10:34

    In 2017, the solution is:

    map.addListener('click', function(e) {
        placeMarker(e.latLng, map);
    });
    
    function placeMarker(position, map) {
        var marker = new google.maps.Marker({
            position: position,
            map: map
        });
        map.panTo(position);
    }
    

提交回复
热议问题