添加大量标记时,Google Maps Android API v2非常慢 [英] Google Maps Android API v2 very slow when adding lots of Markers

查看:100
本文介绍了添加大量标记时,Google Maps Android API v2非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在更新现有的Android应用,以使用新的Google Maps Android API v2.我有大约2500个要添加到地图的标记.使用较旧版本的API,我发现当有2500个标记时,地图的响应能力太差了,因此我不得不通过仅添加地图当前可见区域中的标记来解决它.

我希望新的API可以使2500个标记更快,但是即使在Nexus 4上,它仍然很糟糕,而且我看不到任何进行集群的选项.

所以我的问题是:如何确定地图的可见区域中是否包含某个经度/纬度点?

我查看了 VisibleRegion 在文档中,但到目前为止我还没有运气.任何帮助将不胜感激.

*旁注:即使使用iPhone 3gs,iOS MKMapView(谷歌地图或Apple地图)上的2500个标记也非常平滑且响应迅速.我仍然不明白为什么即使使用最新和最好的硬件,在Android上还是这么慢.

解决方案

好了,在尝试了几件事之后,我弄清楚了如何确定给定点是否在可见区域内,而且非常简单:

//Note: this.mMap is an instance of GoogleMap

LatLngBounds bounds = this.mMap.getProjection().getVisibleRegion().latLngBounds;

LatLng markerPoint = new LatLng(item.getLatitude(), item.getLongitude());

if(bounds.contains(markerPoint))
{
    this.mMap.addMarker(new MarkerOptions(...));    
}

*请注意,获取GoogleMap的投影是一项昂贵的操作,因此,如果您要遍历一长串项目来创建标记并将其添加到地图中(就像我一样),则在循环之前仅获取一次投影

更新

我决定写一篇博客文章,详细介绍如何显示地图可见区域中的标记以及如何将标记移出屏幕来隐藏它们.这不是一个完美的解决方案,但是如果您要显示成千上万个标记,并且知道您的用户不需要同时查看所有标记(除非它们缩小显示),那么这是一个很好的解决方法.

使用Google Maps Android API V2隐藏和显示在屏幕上的标记

I am updating an existing Android app to use the new Google Maps Android API v2. I have about 2500 markers that I want to add to the map. With the older version of the API I found the responsiveness of the maps to be horrendous when there are 2500 markers, so I had to work around it by only adding markers that are in the current visible region of the map.

I was hoping 2500 markers would be faster with the new API, but it's still awful, even on a Nexus 4, and I'm not seeing any sort of option to do clustering.

So my question: how can I determine if a certain lat/lng point is contained within the visible region of the map?

I have looked at VisibleRegion in the documentation, but I've not had any luck with it so far. Any help is greatly appreciated.

*Side note: 2500 markers on an iOS MKMapView (google maps or Apple maps) is very smooth and responsive even with an iPhone 3gs. I still can't understand why it's so slow on Android, even with the latest and greatest hardware.

解决方案

Alright, after trying a couple more things I figured out how to determine if a given point is in the visible region, and it's pretty simple:

//Note: this.mMap is an instance of GoogleMap

LatLngBounds bounds = this.mMap.getProjection().getVisibleRegion().latLngBounds;

LatLng markerPoint = new LatLng(item.getLatitude(), item.getLongitude());

if(bounds.contains(markerPoint))
{
    this.mMap.addMarker(new MarkerOptions(...));    
}

*Note that getting the projection of the GoogleMap is an expensive operation, so if you're looping through a long list of items to create Markers and adding them to the map like I am, only grab the projection once before you loop.

Update

I decided to write up a blog post detailing how to show Markers that are in the visible region of the map and hide Markers as they are moved off the screen. It's not a perfect solution, but if you are showing thousands of Markers and know that your users don't need to see all of them at the same time (unless they zoom way out), it's a pretty good work-around.

Hiding and Showing on screen Markers with Google Maps Android API V2

这篇关于添加大量标记时,Google Maps Android API v2非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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