如何检测 Android 中的 Google MapView 运动?
我想在每次用户将其移动一定距离时动态更新地图/加载新的地图叠加层。我该怎么做呢?每次用户移动地图时是否都有监听器?我很可能只是测量中心点之间的距离。
谢谢!
I'd like to dynamically update the map/ load new map overlays every time my user moves it a certain amount of distance. How do I go about doing this? Is there a listener for every time the user moves the map? Most likely I'd just measure the distance between the center points.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个来检测地图移动:
http://pa.rezendi.com/ 2010/03/responding-to-zooms-and-pans-in.html
在开始时和移动地图后,您应该保存当前的地图中心。正如文章所建议的,跟踪 ACTION_UP 以确定用户何时完成地图移动。然后,将新地图中心与旧地图中心进行比较。要获取地图中心,请使用 MapView 的 getMapCenter():
http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/MapView.html#getMapCenter()
编辑:我对此做了一些额外的工作,并有一个完整的应用程序和代码供您欣赏。这篇博文解释了一个计时器解决方案,并包含 Github 源代码的链接:
http://bricolsoftconsulting.com/extending-mapview-to-add -a-更改事件/
Try this to detect the map movements:
http://pa.rezendi.com/2010/03/responding-to-zooms-and-pans-in.html
On start and after the map has been moved, you should save the current map center. As the article suggests, track ACTION_UP to determine when the user has finished a map movement. Then, compare the new map center with the old map center. To get the map centers use MapView's getMapCenter():
http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/MapView.html#getMapCenter()
EDIT: I have done some additional work on this, and have a complete application with code for your enjoyment. This blog post explains a timer solution to this and contains a link to Github source:
http://bricolsoftconsulting.com/extending-mapview-to-add-a-change-event/
遗憾的是,地图界面不包含任何监听器来监视地图操作(缩放/平移)。您需要设置某种计时器来检查中心/缩放级别,以确定情况是否发生变化并相应更新。
The map interface unfortunately does not include any listeners to monitor map actions (zoom/pan). You'll need to set up a timer of some sort that checks the center/zoom levesl to figure out if things have changed and update accordingly.
您可以通过重写
Activity
中的onTouch(...)
方法来实现此目的(以便每次用户平移地图时,您都可以重新计算地图边界)。您需要知道地图的缩放级别和边界等,以便根据地图距离加载这些叠加层。You could do this by overriding the
onTouch(...)
method in yourActivity
(so that every time the user pans the map, you could recalculate the map boundaries). You would need to know the map's zoom level and boundaries, etc, in order to load these overlays based on map distances.