如何在Google地图上收听用户生成的缩放? [英] How to listen for user generated zoom in Google Maps?

查看:184
本文介绍了如何在Google地图上收听用户生成的缩放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道Google Maps zoom_changed事件何时被用户与+/-缩放按钮交互特别触发。如果我为zoom_changed使用通用事件侦听器,则无法判断它是由用户生成的事件还是由类似fitBounds()引起的缩放更改。寻找最好的方式来做到这一点。



我试过以下的东西,其中没有一个可以工作:

1)寻找事件有关zoom_changed的信息。似乎没有。

2)为mouseover和mouseout添加侦听器,让我设置一个标志来查看用户是否位于地图边界内,并检查标志在zoom_changed上。这不起作用,因为地图不会将缩放按钮视为地图框架的一部分(换句话说,将鼠标悬停在缩放按钮上会触发mouseout事件)。



3)添加一个普通的(非gMap)侦听器到缩放按钮。然而,我找不到一个权威的CSS选择器,它可以让我抓住按钮。



4)在gMaps API中寻找一个函数让我做类似getZoomElements()的事情,然后我可以使用它设置监听器。

奇怪的是,如果我将自定义控件添加到地图,我可以清楚地做我想做的事。它似乎很奇怪,他们会强迫我这样做,而不是有一个挂钩到默认的缩放控制。

解决方案

我wouldn不要直接连接到 + / - 按钮(或者您自己的自定义控件上的按钮)。用户可以使用鼠标滚轮更改地图上的缩放比例,或双击地图。此外,您将依赖实现细节而不是记录的API,这是主要禁止

这确实意味着唯一的可靠的记录方式来检测缩放的变化,即收听 <$ Map 的c $ c> zoom_changed 事件



如果您的事件处理程序无法确定事件是来自用户操作还是API调用,有两种方法:




  • 在调用之前设置一个标志API函数,以便您知道您可以忽略此更改。

  • 您是否可以重新构建应用程序,以便缩放更改是来自代码还是用户? li>

I want to know when a Google Maps zoom_changed event is fired specifically by a user interaction with the +/- zoom buttons. If I use a general event listener for zoom_changed, I can't tell if it is a user-generated event or a zoom change caused by something like fitBounds(). Looking for the best way to do this.

I've tried the following things, none of which seem to work:

1) Looked for event information on zoom_changed. There appears to be none.

2) Add listeners for mouseover and mouseout that let me set a flag to see if the user is in the map bounds, and check the flag on zoom_changed. This doesn't work because the map does not consider the zoom buttons as part of the map frame (in other words, hovering over zoom buttons triggers the mouseout event).

3) Add a normal (non-gMap) listener to the zoom buttons. However, I can't find a definitive CSS selector that will allow me to grab just the buttons.

4) Looked for a function in the gMaps API that would let me do something like getZoomElements(), and then I could set listeners using that.

The weird thing is I can clearly do what I want if I add a custom control to the map. It seems very odd that they would force me to do that instead of having a hook into the default zoom controls.

解决方案

I wouldn't just hook in to the +/- buttons (or buttons on your own custom control for that matter). The user can change the zoom on the map with the mouse wheel, or by double-clicking on the map. Plus, you'd be relying on implementation detail rather than documented API, which is a major no-no.

This really means the only reliable, documented way to detect a change in zoom is to listen to the zoom_changed event of the Map.

If your event handler can't determine whether the event came from user action or an API call, there's two approaches:

  • Set a flag before calling an API function so that you know you can ignore this change.
  • Can you re-architect your app such that it does not matter whether a zoom change came from code or the user?

这篇关于如何在Google地图上收听用户生成的缩放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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