简体   繁体   English

dragend事件监听器无法在Google Maps API v3中触发?

[英]dragend event listener not firing in Google Maps API v3?

It won't even log to the console. 它甚至不会登录到控制台。 I'm not sure what's going on here: 我不确定这是怎么回事:

var mapOptions = {
  zoom: 5,
  center: new google.maps.LatLng(-41.2, 173.3),
  mapTypeId: google.maps.MapTypeId.ROADMAP,
  streetViewControl: false
}

var homeMarker = new google.maps.Marker();

var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

google.maps.event.addListener(map, "click", function(event) {
// If the marker already exists, set it's position to null
    if (homeMarker) {
        homeMarker.setPosition(null);
    }

// Instantiate the marker again
  homeMarker = new google.maps.Marker({
    position: event.latLng,
    map: map,
    title: 'Your Home Location',
    draggable: true 
  });

// Set the input fields to the latitude and longitude
    var latitude = event.latLng.lat();
    var longitude = event.latLng.lng();

    $('input#homeLat').val(latitude.toFixed(6));
    $('input#homeLong').val(longitude.toFixed(6));
 }); 

// What is wrong here?
google.maps.event.addListener(homeMarker, "dragend", function(event) {
 console.log('foo');        
});

Basically, what is meant to happen is that I want to fetch the coordinates from the new position of the marker, which can be changed via a click on the map and a drag of the marker itself. 基本上,这意味着要从标记的新位置获取坐标,可以通过单击map和拖动标记本身来更改坐标。 I've got the former working, but my eventListener for dragend doesn't seem to be firing at all. 我已经使用了前者,但是我的eventListenerdragend似乎根本没有触发。 Any ideas? 有任何想法吗?

Alert while dragging, same code works for me. 拖动时发出警报,相同的代码对我有效。 getPosition() helps to get the current marker position getPosition()有助于获取当前标记位置

Try this, 尝试这个,

google.maps.event.addListener(homeMarker, "dragend", function(event) {
     alert('map dragged');
     homeMarker.getPosition();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM