代码改变世界

google map bug: dragend事件被触发时,click也被触发

2012-04-19 14:21  iBlog  阅读(1396)  评论(0编辑  收藏  举报

 在IE浏览器下,google地图上触发dragend事件时click也会被触发,在FIREFOX,CHROME上均不会出现这样的情况。

gmaps-api-issues上有人报BUG

Issue 4072: Bug: Dragend Event also trigger Click Event in IE

在stackoverflow上有一个解决方法:http://stackoverflow.com/questions/10099961/google-map-bug-dragend-event-also-trigger-click-event-in-ie

我呢,外打正着,在dragend事件处理函数中,加入了alert();语句,问题就解决了!遗憾的是不知道为什么就解决了。测试代码如下:

 1 var map;
 2 var myOptions = {
 3     zoom: 6,
 4     center: new google.maps.LatLng(46.87916, -3.32910),
 5     mapTypeId: 'terrain'
 6 };
 7 map = new google.maps.Map($('#map')[0], myOptions);
 8 var marker = new google.maps.Marker({
 9     map: map,
10     position: new google.maps.LatLng(46.87916, -3.32910),
11     draggable: true
12 });
13 google.maps.event.addListener(marker, 'dragend', function (e) {
14     console.log('dragend'); 
15     alert('dragend');
16 });
17 
18 google.maps.event.addListener(map, 'click', function (evt) {
19     console.log(evt);
20     console.log('click'); 
21     alert('click');
22 });