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

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

问题描述

我正在更新现有的 Android 应用以使用新的 Google Maps Android API v2.我有大约 2500 个标记要添加到地图中.使用旧版本的 API 时,我发现当有 2500 个标记时地图的响应速度非常糟糕,因此我不得不通过仅添加地图当前可见区域中的标记来解决这个问题.

我希望使用新 API 时 2500 个标记会更快,但它仍然很糟糕,即使在 Nexus 4 上也是如此,而且我没有看到任何进行聚类的选项.

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

我看过VisibleRegion在文档中,但到目前为止我还没有运气.非常感谢任何帮助.

*旁注:即使使用 iPhone 3gs,iOS MKMapView(谷歌地图或苹果地图)上的 2500 个标记也非常流畅和响应.我仍然无法理解为什么它在 Android 上如此缓慢,即使使用最新最好的硬件.

解决方案

好吧,在尝试了几件事之后,我想出了如何确定给定点是否在可见区域内的方法,而且非常简单:

//注:this.mMap是GoogleMap的一个实例LatLngBounds 边界 = this.mMap.getProjection().getVisibleRegion().latLngBounds;LatLng 标记点 = 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天全站免登陆