Issue
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.
Solution
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
Answered By - DiscDev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.