Google Maps drag and dragend event listeners won't work if marker created by click event listener

后端 未结 3 2017
傲寒
傲寒 2020-12-25 11:05

Just a noob trying to learn, I came out with a problem that when I try to create marker with click event, drag and dragend event listeners won\'t work. Whereas when created

相关标签:
3条回答
  • 2020-12-25 11:39
    var marker = new google.maps.Marker({
            position: latlng,
            map: map,
            zoom:25,
            title: title,
            draggable:true,
    });
    var infowindow = new google.maps.InfoWindow({
        content: info 
      });
    infowindow.open(map,marker);
    
    google.maps.event.addListener(marker,'drag',function(event) {
        document.getElementById('lat').value = event.latLng.lat();
        document.getElementById('lng').value = event.latLng.lng();
        var infowindow = new google.maps.InfoWindow({
            content: 'Latitude: ' + event.latLng.lat() + '<br>Longitude: ' + event.latLng.lng()
          });
        infowindow.open(map,marker);
    });
    
    google.maps.event.addListener(marker,'dragend',function(event) 
            {
        document.getElementById('lat').value =event.latLng.lat();
        document.getElementById('lng').value =event.latLng.lng();
        var infowindow = new google.maps.InfoWindow({
            content: 'Latitude: ' + event.latLng.lat() + '<br>Longitude:'+`event.latLng.lng()
          });
        infowindow.open(map,marker);
    });
    
    0 讨论(0)
  • 2020-12-25 11:57
    var map;
    function initialize() {
      var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
      var myOptions = {
        zoom: 4,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    
      google.maps.event.addListener(map, 'click', function(event) {
        placeMarker(event.latLng);
      });
    }
    
    function placeMarker(location) {
      var marker = new google.maps.Marker({
          position: location,
          draggable:true,
          map: map
      });
    
      map.setCenter(location);
    }
    

    Try creating the marker like this .

    0 讨论(0)
  • 2020-12-25 11:59

    What you probably want to do is change the reference in your event listeners, from this to the event itself.

    Then you need to add separate event listeners for your new marker at the same time as you create it. I'd suggest simply having one function which takes input arguments for the latlng, title, and anything else you need. And it then creates the marker and any event listeners required.

    function initialize() {
        var myLatlng = new google.maps.LatLng(22,79);
        var myOptions = {
          zoom: 5,
          center: myLatlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }
    
        var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    
        addMarker(myLatlng, 'Default Marker', map);
    
        map.addListener('click',function(event) {
            addMarker(event.latLng, 'Click Generated Marker', map);
        );
    }
    
    function handleEvent(event) {
        document.getElementById('lat').value = event.latLng.lat();
        document.getElementById('lng').value = event.latLng.lng();
    }
    
    function addMarker(latlng,title,map) {
        var marker = new google.maps.Marker({
                position: latlng,
                map: map,
                title: title,
                draggable:true
        });
    
        marker.addListener('drag', handleEvent);
        marker.addListener('dragend', handleEvent);
    }
    
    0 讨论(0)
提交回复
热议问题