0

我正在使用谷歌地图,我有这个_marker()功能。我目前正在使用setTimeout6 秒来延迟回调,以便它在标记被删除后运行。我一直在查看文档并尝试了类似gmaps.event.addListenerOnce(marker, 'idle', function(...没有运气的方法。有谁知道标记动画放置事件,所以我可以使这个回调合法化?

var _marker = function(place, map, callback){
    var marker = new gmaps.Marker({
        clickable: false,
        draggable: false,
        position: new gmaps.LatLng(place.latitude, place.longitude),
        map: map,
        animation: gmaps.Animation.DROP,
        icon: new gmaps.MarkerImage(
            'http://maps.google.com/mapfiles/ms/micons/red-dot.png',
            new gmaps.Size(32, 32),
            new gmaps.Point(0,0),
            new gmaps.Point(16, 32)
        )
    });
    if(typeof callback !== "undefined"){
        setTimeout(function(){
            return callback(marker);
        }, 600);
    }else{
        return marker;
    }
}
4

1 回答 1

0

这是一个 delayMarker 函数的工作方式与原始函数相同,除非有一种方法允许您创建标记而不显示它,然后显示并动画它,我不确定是否有更好的方法。

JSBIN 演示

function initMap() {
  var myLatLng = {lat: -25.363, lng: 131.044};

  var googleMapsIcon = new google.maps.MarkerImage(
    'http://maps.google.com/mapfiles/ms/micons/red-dot.png',
    new google.maps.Size(32, 32),
    new google.maps.Point(0,0),
    new google.maps.Point(16, 32)
  )

  function delayMarker ({place = {}, position, map, timeout}, callback) {

    var marker = {
      clickable: false,
      draggable: false,
      position: new google.maps.LatLng(position.lat, position.lng),
      map: map,
      animation: google.maps.Animation.DROP,
      icon: googleMapsIcon
    }

    if (timeout && callback) {
      return setTimeout(function(){
        return callback(new google.maps.Marker(marker))
      }, timeout)  
    } else {
      return marker  
    }
  }

  var map = new google.maps.Map(document.getElementById('map-canvas'), {
    center: myLatLng,
    scrollwheel: false,
    zoom: 4
  })

  var marker = delayMarker({
    map: map,
    position: myLatLng,
    timeout: 2000,
  }, () => {
    console.log('done')
  })

} 
于 2016-04-08T02:40:28.770 回答