Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 455
DiscDev Avatar asked Dec 04 '12 22:12

DiscDev


People also ask

How many markers can Google Maps API handle?

2048 characters in URL is just under 100 GeoCode values. So, again no more than 100 markers.

Why is Google Maps lagging so much?

Why is Google Maps so slow? It can be caused by several reasons – the outdated version of Google Mops, Google Maps data cache, incompatibility with your device, etc. Now, let's see how to fix the Google Maps lagging issue on Android /Windows.

How do I add multiple markers to Google Maps API?

Follow the below steps and add/show multiple markers on google maps using javascript with infowindows: Step 1 – Create HTML File For Display Multiple Markers. Step 2 – Add Google Maps API V3 in HTML. Step 3 – Implement JavaScript Function To Create Markers/Pins And Show on Google Map.


2 Answers

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

like image 84
DiscDev Avatar answered Sep 29 '22 07:09

DiscDev


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

like image 29
MaciejGórski Avatar answered Sep 29 '22 07:09

MaciejGórski