谷歌地图的双击事件传播

2023-04-19前端开发问题
8

本文介绍了谷歌地图的双击事件传播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

<块引用>

这个问题经常被问到,但从来没有真正得到很好的回答.让我们看看我们是否可以解决它!

事件传播

Google 允许您通过他们的 API 使用 事件处理程序.p>

有时您可能会将您的事件处理程序绑定到 Google本身 已经绑定到的事件.因此,当您的事件触发并执行您让它执行的任何操作时,您可能会发现 Google 同时也在做自己的小事.

<块引用>

嗯,我可以处理该事件以使我的代码运行,但阻止该事件继续并触发 Google 的事件处理程序吗?

你当然可以!欢迎使用 Event Propagation(又名事件冒泡).

看看这段代码

这里我绑定了一个事件处理程序来双击谷歌地图:

var aListener = google.maps.event.addListener(map, 'dblclick', function(event) {//尽量防止事件传播到地图事件.stop();event.cancelBubble = true;if (event.stopPropagation) {event.stopPropagation();}if (event.preventDefault) {event.preventDefault();} 别的 {event.returnValue = 假;}});

这里 map 是要绑定到的 Google Map 对象.

这不起作用.事件冒泡,地图放大.我不知道为什么.

<块引用>

你问,你读过文档吗?

确实如此.documentation 说要使用 event.stop();

我看过其他人在说什么.这个问题正是我的问题.它被标记为已修复,但解决方案不起作用.

想法?

解决方法

双击事件的一种可能解决方法是在您需要它不触发时禁用 Google 的默认行为,然后稍后重新启用它.

您可以使用 disableDoubleClickZoom 参数来执行此操作.请参阅此处的文档.

这里有一些代码可以禁用:

map.set("disableDoubleClickZoom", true);

现在重新启用:

map.set("disableDoubleClickZoom", false);

当然,您可以在 MapOptions 参数中设置属性,以便首先创建 map 对象.

解决方案

更新: 不幸的是,我不得不发现 Firefox 无法通过以下方式访问当前事件window.event 因此此代码在那里不起作用.我还没有找到解决方法.

<小时>

事实证明,对代码的修复很少:只需从事件处理函数中删除 event 参数,从而访问处理程序内的全局 window.event 对象.

以下示例代码在 IE 和 Chrome 中适用于我,但 不适用于 Firefox:

google.maps.event.addListener(map, "dblclick", function(googleMapsEvent) {console.debug("抓到双击");//引用全局事件对象//忽略谷歌地图传入的 googleMapsEvent!event.preventDefault();});

这个答案让我走上了正轨!

This question is asked often, but never really answered well. Let's see if we can remedy it!

Event Propagation

Google allows you to bind to events in a Google Map View via their API using event handlers.

Sometimes you may bind your event handler to an event that Google itself is already bound to. Thus, when your event fires and does whatever you told it to do you may find Google also doing its own little thing at the same time.

Hmm, can I handle the event so my code runs, but stop the event from continuing on and firing Google's event handler?

You sure can! Welcome to Event Propagation (aka Event Bubbling).

Take a look at this code

Here I bind an event handler to double clicking on the Google Map:

var aListener = google.maps.event.addListener(map, 'dblclick', function(event) {
    // Try to prevent event propagation to the map
    event.stop();
    event.cancelBubble = true;
    if (event.stopPropagation) {
        event.stopPropagation();
    }
    if (event.preventDefault) {
        event.preventDefault(); 
    } else {
        event.returnValue = false;  
    }
});

Here map is a Google Map object to bind to.

This doesn't work. The event bubbles, and the map zooms in. I don't know why.

You ask, have you read the documentation?

Indeed. The documentation says to use event.stop();

I have looked at what others are saying. This issue is exactly my problem. It was marked as fixed, but the solution does not work.

Ideas?

Workaround

A possible workaround for the doubleclick event is to disable Google's default behavior when you need it to not fire, and then re-enable it later.

You do this with the disableDoubleClickZoom argument. See the documentation here.

Here is some code to disable:

map.set("disableDoubleClickZoom", true);

Now to re-Enable:

map.set("disableDoubleClickZoom", false);

Of course, you can set the property in the MapOptions argument for when the map object is created in the first place.

解决方案

UPDATE: Unfortunately, I had to find out that Firefox does not make the current event accessible via window.event thus this code won't work there. I haven't found a workaround for that, yet.


It turns out the fix to your code is minimal: just remove the event parameter from your event handler function, thus accessing the global window.event object inside the handler.

The following example code worked for me in IE and Chrome, but not Firefox:

google.maps.event.addListener(map, "dblclick", function(googleMapsEvent) {
    console.debug("caught double click");
    // reference the global event object
    // ignore the googleMapsEvent passed in by Google Maps!
    event.preventDefault();
});

This answer put me on the right track!

这篇关于谷歌地图的双击事件传播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Fatal error: Call to a member function fetch_assoc() on a no
业务场景:使用update语句去更新数据库字段。 原因:update接收值不正确。原来代码: $query = "UPDATE student SET date = now() WHERE id = $id";$result = $mysqli-query($query2) or die($mysqli-error); // 问题出现了在这句 $data = $result-fetch_ass...
2024-12-13 前端开发问题
136

layui实现laydate日历控件控制之前日期不可选择
具体实现代码如下: laydate.render({ elem: '#start_time', min:0, //,type: 'date' //默认,可不填}); 只要加一个min参数,就可以控制了。0表示之前的日期不可...
2024-11-29 前端开发问题
133

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313