GoogleMaps v3 API Create only 1 marker on click

后端 未结 5 2013
孤城傲影
孤城傲影 2020-12-01 10:30

I am successfully creating a marker on click however, using the following code, I get a new marker with every click, I only ever want one marker added and if someone clicks

相关标签:
5条回答
  • 2020-12-01 10:57

    You can try to make it directly in your listener:

    google.maps.event.addListener(Map, 'click', function(event){
        marker.setMap(null);    
        marker = new google.maps.Marker({
            map:       Map,
            position:  event.latLng
        });     
    });
    
    0 讨论(0)
  • 2020-12-01 10:58
    var marker;
    
    function placeMarker(location) {
      if ( marker ) {
        marker.setPosition(location);
      } else {
        marker = new google.maps.Marker({
          position: location,
          map: map
        });
      }
    }
    
    google.maps.event.addListener(map, 'click', function(event) {
      placeMarker(event.latLng);
    });
    

    You have to work on the same marker all the time - do not create new ones. Here you have a global variable marker and in placeMarker function you assign marker to this variable first time. Next time it checks that marker exists already and then just changes its position.

    0 讨论(0)
  • 2020-12-01 11:00

    use addListenerOnce, instead of addListener

    0 讨论(0)
  • 2020-12-01 11:16
    <div id="map" style="width:100%;height:500px;"></div>
    
    <script>
    
        var marker;
        var infowindow;
    
        function myMap() {
            var mapCanvas = document.getElementById("map");
            var myCenter=new google.maps.LatLng(51.508742,-0.120850);
            var mapOptions = {
                center: myCenter, zoom: 18,
                mapTypeId:google.maps.MapTypeId.HYBRID
            };
            var map = new google.maps.Map(mapCanvas, mapOptions);
            google.maps.event.addListener(map, 'click', function(event) {
                placeMarker(map, event.latLng);
            });
        }
    
        function placeMarker(map, location) {
            if (!marker || !marker.setPosition) {
                marker = new google.maps.Marker({
                    position: location,
                    map: map,
                });
            } else {
                marker.setPosition(location);
            }
            if (!!infowindow && !!infowindow.close) {
                infowindow.close();
            }
            infowindow = new google.maps.InfoWindow({
                content: 'Latitude: ' + location.lat() + '<br>Longitude: ' + location.lng()
            });
            infowindow.open(map,marker);
        }
    
    
    </script>
    
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkG1aDqrbOk28PmyKjejDwWZhwEeLVJbA&callback=myMap"></script>
    

    this works fine jsfiddle http://jsfiddle.net/incals/o7jwuz12

    and this for complete code: https://github.com/Lavkushwaha/Google_Maps_API_Without_PHP_DOM/blob/master/single.html

    0 讨论(0)
  • 2020-12-01 11:21

    Hello I stumbled upon the same problem and I came up with this answer before finding this post. So here is the answer:

    add the eventListener inside the map initialize function:

    function initialize() {
          ...
          google.maps.event.addListener(map, 'click', function(event) {
            addListing(event.latLng);
          });
    }
    

    then create a counter variable and use it inside addListing variable (my function to add the marker):

    var i=0
    function addListing(location) {
      var addMarker;
      var iMax=1;
    
      if(i<=iMax) {
          addMarker = new google.maps.Marker({
          draggable:false,
          position: location,
          map: map
      });
    
      google.maps.event.addListener(addMarker, 'dblclick', function() {
        addMarker.setMap(null);
        i=1;
      });
    
      i++;
    
      } else {
          console.log('you can only post' , i-1, 'markers');
          }
    }
    

    The other benefit of this approach( I think) is that you can control the amount of markers the user can place in the map easily by increasing or decreasing the max counter value (i.e. iMax=2 will increase the amount of allowed markers to 2). You erase the marker by double clicking it and you can re-place it in another location.

    Hope it helps anyone in the future.
    Cheers

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