【问题标题】:Google map api v3 "dragend" event fired more than onesGoogle map api v3“dragend”事件触发了不止一个
【发布时间】:2016-10-25 04:41:38
【问题描述】:

目前我的谷歌地图(api v3)上有这个监听器事件:

google.maps.event.addListener(Map, 'center_changed', FixedMarkerInCenter);
google.maps.event.addListener(Map, 'zoom_changed', FixedMarkerInCenterZoom);
google.maps.event.addListener(Map, 'dragend', FindReverseGeocode });

问题在于“dragend”事件触发了多次(至少四次),并且函数“FindReverseGeocode”发生了很多次。 有谁知道这个问题吗?

【问题讨论】:

  • 我猜你在“FixexMarkerInCenter”和“FixedMarkerInCenterZoom”中都调用了“FindReverseGeocode”,这导致对“FindReverseGeocode”的调用很多。

标签: google-maps-api-3


【解决方案1】:

的确,这很烦人,但是……不要绝望——你可以用简单的 JavaScript 来修复它:

map.dragInProgress = false; //adding flag to already existing map object to keep DOM clean
google.maps.event.addListener(map, 'dragend', function() {
  if(map.dragInProgress == false) { //only first shall pass
    map.dragInProgress = true;
    window.setTimeout(function() {
        console.log('Note how you will see this console message only once.');
        //cast your logic here
        map.dragInProgress = false; //reset the flag for next drag
    }, 1000);
  }
});

简而言之,这允许您每秒仅接收一次 dragend 事件。确保你的脚本不会在你的逻辑中间死掉,否则第一个将是最后一个拖累。你可以使用 try/catch/finally 来克服这个问题。尽情享受吧!

【讨论】:

    【解决方案2】:

    我支持我的评论,但这里是一个基本地图,它实现了 center_changed(带有标记)和 dragend 带有地理编码器。

    在 IE9、Chrome 19 和 Firefox 13 中,dragend 事件每次拖动只会触发一次。您会注意到 center_changed 被触发了很多次,即使在拖动未完成时也是如此。这就是为什么我相信您的 FindReverseGeocode 方法在 FixedMarkerInCenter 中被调用。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js' type='text/javascript'></script>
    <script type="text/javascript"></script>
    </head>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript">
    
    var map;
    var geocoder;
    
    function initialize()
    {
        var latlng = new google.maps.LatLng(47.6059399181561, 14.747812500000007);
        map = new google.maps.Map(document.getElementById('map'), {
          zoom:6,
          center:latlng,
          mapTypeId:google.maps.MapTypeId.HYBRID
        });
    
        google.maps.event.addListener(map, 'center_changed', placeMarker);
        google.maps.event.addListener(map, 'dragend', reverseLookup);
    
        geocoder = new google.maps.Geocoder();
    }
    
    function placeMarker()
    {
        var center = map.getCenter();
        marker = new google.maps.Marker({
          map:map,
          position: center
        });
    }
    
    function reverseLookup()
    {
        geocoder.geocode({'latLng': map.getCenter()}, function(results, status)
        {
          if (status == google.maps.GeocoderStatus.OK)
          {
            document.getElementById('area').value = 
            results[results.length-1].formatted_address + " " +
            map.getCenter() + "\r\n" + document.getElementById('area').value;
          }
        });
    }
    
    </script>
    <body onLoad="initialize()">
    <div id="map" style="width: 800px; height: 350px;"></div>
    <textarea id='area' cols='80' rows='5'></textarea>
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-19
      • 2013-08-25
      • 2013-02-10
      • 2014-05-25
      • 2013-11-07
      • 2011-09-23
      • 1970-01-01
      相关资源
      最近更新 更多