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

时间:2013-09-09 08:37:00

标签: javascript google-maps-api-3 google-maps-markers

它甚至不会登录到控制台。我不确定这里发生了什么:

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');        
});

基本上,我想要从标记的新位置获取坐标,可以通过单击map并拖动标记本身来更改坐标。我有前者工作,但eventListener dragend似乎根本没有开火。有什么想法吗?

1 个答案:

答案 0 :(得分:7)

拖动时发出警报,同样的代码对我有效。 getPosition()有助于获取当前标记位置

试试这个,

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