Add marker to Google Map on Click

前端 未结 6 1917
有刺的猬
有刺的猬 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条回答
  •  -上瘾入骨i
    2020-12-12 10:47

    This is actually a documented feature, and can be found here

    // This event listener calls addMarker() when the map is clicked.
      google.maps.event.addListener(map, 'click', function(e) {
        placeMarker(e.latLng, map);
      });
    
      function placeMarker(position, map) {
        var marker = new google.maps.Marker({
          position: position,
          map: map
        });  
        map.panTo(position);
      }
    

提交回复
热议问题