Google Maps Android API v2 very slow when adding lots of Markers

喜你入骨 提交于 2019-11-27 17:27:31

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

For a clustering solution on Android you may want to try Android Maps Extensions: https://github.com/mg6maciej/android-maps-extensions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!