Google maps api 3 谷歌地图api v3“;“德拉根德”;事件引发了多个事件

Google maps api 3 谷歌地图api v3“;“德拉根德”;事件引发了多个事件,google-maps-api-3,Google Maps Api 3,目前,我的谷歌地图(api v3)上有此侦听器事件: 问题是“dragend”事件触发了不止一次(至少四次),函数“FindReverseGeocode”发生了很多次。 有人知道这个问题吗?我同意我的意见,但这里有一个基本地图,它实现了中心更改(带标记)和dragend 在IE9、Chrome 19和Firefox 13中,dragend事件每次拖动仅触发一次。您会注意到,center\u changed被多次激发,即使拖动尚未完成。这就是为什么我相信您的FindReverseGeocode方法

目前,我的谷歌地图(api v3)上有此侦听器事件:

问题是“dragend”事件触发了不止一次(至少四次),函数“FindReverseGeocode”发生了很多次。
有人知道这个问题吗?

我同意我的意见,但这里有一个基本地图,它实现了
中心更改
(带标记)和
dragend

在IE9、Chrome 19和Firefox 13中,
dragend
事件每次拖动仅触发一次。您会注意到,
center\u 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>

var映射;
var地理编码器;
函数初始化()
{
var latlng=新的google.maps.latlng(47.6059399181561,14.74781250000007);
map=new google.maps.map(document.getElementById('map'){
缩放:6,
中心:拉特林,
mapTypeId:google.maps.mapTypeId.HYBRID
});
google.maps.event.addListener(映射,'center_changed',placeMarker);
google.maps.event.addListener(map'dragend',reverseLookup);
geocoder=新的google.maps.geocoder();
}
函数placeMarker()
{
var center=map.getCenter();
marker=新的google.maps.marker({
地图:地图,
位置:中
});
}
函数reverseLookup()
{
geocoder.geocode({'latLng':map.getCenter()},函数(结果,状态)
{
if(status==google.maps.GeocoderStatus.OK)
{
document.getElementById('area')。值=
结果[results.length-1]。格式化的\u地址+“”+
map.getCenter()+“\r\n”+document.getElementById('area')。值;
}
});
}

没错,这很烦人,但。。现在不要绝望-您可以用简单的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来克服这个问题。享受吧

我猜您在“FixexMarkerInCenter”和“FixedMarkerInCenterZoom”中都调用了“FindReverseGeocode”,这会导致大量调用“FindReverseGeocode”。
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);
  }
});