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

后端 未结 3 2016
傲寒
傲寒 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: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 .

提交回复
热议问题