在地图平移时避免自定义覆盖点击事件(谷歌地图API) [英] Avoid custom overlay click event when map is panning (google maps API)

查看:119
本文介绍了在地图平移时避免自定义覆盖点击事件(谷歌地图API)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与此问题相关:'制作自定义叠加层可点击( Google Maps API 3)',也与我的在同一个问题(张贴在这里给它更多的知名度,因为我认为是其他问题)。

Related to this question: 'Make custom overlay clickable (Google Maps API 3)' and related also to my comment in the same question (posted here to give it more visibility and because I think is other problem).

我添加了一个点击监听器到我的覆盖多边形,但现在我遇到了这样的问题,即当用户想要平移地图并单击覆盖图来做到这一点时,当鼠标按钮被释放时,点击事件是触发。显然,我不想在执行 onclick 操作时只想平移地图。这个问题的任何优雅的解决方案?

I have added a click listener to my overlay polygon but now I have the problem that when user wants to pan the map and clicks on an overlay to do that, when the mouse button is released the click event is triggered. Obviously I don't want to execute the onclick action when I just want to pan the map. Any elegant solution for this issue?

这是一个问题的例子:平移/点击问题

Here is an example of the issue: panning/click issue.

推荐答案

查看此更新的提琴: http://jsfiddle.net/9gvsq3od/5/

基本上我添加了这段代码:

Basically I added this code:

 var dragging = false;

 google.maps.event.addDomListener(this.path_, 'mousedown', function (evt) {
     dragging = false;
 });

 google.maps.event.addDomListener(this.path_, 'mousemove', function (evt) {
     dragging = true;
 });

 // onclick listener
 google.maps.event.addDomListener(this.path_, 'click', function (evt) {
     if (dragging) { return false;} 
     alert('clicked on path');
 });

只有当鼠标按钮被释放时,click事件才会被触发,所以代码会设置一个变量<$ c $当鼠标移动时,拖动为真。第一个 mousedown 句柄会重置拖动变量,因为没有mousestop事件,所以我们需要在开始新的交互时重置状态。

The click event is only triggered when the mouse button is released so code sets a variable dragging to true when the mouse moves. The first mousedown handle resets the dragging variable, since there is no "mousestop" event, we need to reset the state when beginning a new interaction instead.

这篇关于在地图平移时避免自定义覆盖点击事件(谷歌地图API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆