Avoid custom overlay click event when map is panning (google maps API)

空扰寡人 提交于 2019-12-08 12:41:58

问题


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).

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.


回答1:


Check out this updated fiddle here: 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');
 });

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.



来源:https://stackoverflow.com/questions/27504500/avoid-custom-overlay-click-event-when-map-is-panning-google-maps-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!