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

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

Leave a Comment