Map dragging is slow when using more than 15 000 markers in a high zoom level
Hey, I have (sometimes) much more than 15 000 markers. When i drag the map in a high zoom level e.g. above an area that has only like 200 (clustered) markers it is very slow although I have a Galaxy S3. Someone knows how to make dragging fast in high zoom levels?
I think I know why: the markers are distributed all over the the earth and clusters are calculated for all of them although only a small area is visible for the user. So imho a fix would be that clusters are only computed in the visible area.
Thanks for reporting!
This is roughly fixed in HEAD, but needs more work, and is why v0.2 (which includes clustering) hasn't been widely publicised. Stand by. :-)
Also, very curious what you're doing with 15K markers - feel free to mail me privately if you don't want to talk about it here.
Seems like adding a simple if (onScreen) before line 353 in DefaultClusterRenderer.java fixes the problem. But I'm unsure what other implications this has..
Working on this next.
Hey, I have 3600 markers and when I zoom the map in a high zoom level it is very slow although I have a Nexus 5. When zooming is it possible to explode only visible clusters ( clusters on screen ) ? I think it will improve performances.
I have encountered a similar problem with this myself, but it was with polylines. And here's what I did. 1.) In my activity/fragment, i have a SparseArray variable, which holds all the coordinate set (not Polyline yet) 2.) I added a listener to the camera change event onCameraChange such that when this event occurs, I fire off a local AsyncTask, to solve which coordinate set(s), are touched/inside the map's bounds. The map's bounds can be solved by calling map.getProjection().getVisibleRegion().latLngBounds. In this AsyncTask, I perform a tight loop (or something smarter) on my sets of coordinates to find out which ones should be displayed 3.) On the AsyncTask's onPostExecute method, I create the Polyline(s) for those coordinate sets, that are in, or touched by the map bounds (returned by doInBackground...
I'm sure for markers, this can be implemented, or maybe implemented already in the library...
Thank you. Using the idea of your solution helped me improve performances.
I also have implemented @ericpanorel's solution to greatly improve the performance of clustering with a large data set.
I'm using a custom onCameraChange listener that grabs the bounds of the projection, asynchronously calculates a set of points that are within the new bounds, and then clears & adds the new points to the ClusterManager instance. I should note that I am scaling the bounds from the projection up by a factor of 1.5, this makes the user experience a fair bit nicer (clusters visible on screen don't change so rapidly compared with not scaling the bounds).
I would be interested to know if my workaround is the right way of doing things, seems to me like this functionality should be baked into the clustering algorithm somehow?
Thanks.
could anyone of you post the code for doing this? I think it'd help many of us!
Ok I tried it myself. The following code clears all items from the clustermanager and then dynamically checks if markers are in the visible area of the map after a camera position change. If they are, they are added to the clustermanager. This happens in an asynctask and the performance acceptable (at least on a Nexus 7) with ~ 20.000 Markers: // setup camera change listener to fire the asynctask mGoogleMap.setOnCameraChangeListener(new OnCameraChangeListener() { @Override public void onCameraChange(CameraPosition position) { new DynamicallyAddMarkerTask().execute(mGoogleMap.getProjection().getVisibleRegion().latLngBounds); } });
private class DynamicallyAddMarkerTask extends AsyncTask<LatLngBounds, Void, Void> { protected Void doInBackground(LatLngBounds... bounds) { mClusterManager.clearItems(); for (Marker currentMarker : listOfMarkers) { if (bounds[0].contains(currentMarker.mLocation)) { mClusterManager.addItem(currentMarker); } } return null; }
@Override protected void onPostExecute(Void result) { mClusterManager.cluster(); } }
storkme: how did you scale the bounds to be 1.5 times larger than the visible screen area? I tried this: final LatLngBounds screenBounds = mGoogleMap.getProjection().getVisibleRegion().latLngBounds; float[] screenDiameterInMeters = new float[1]; Location.distanceBetween(screenBounds.northeast.latitude, screenBounds.northeast.longitude, screenBounds.southwest.latitude, screenBounds.southwest.longitude, screenDiameterInMeters); screenDiameterInMeters[0] *= .5f; final LatLngBounds.Builder builder = new LatLngBounds.Builder(); builder.include(SphericalUtil.computeOffset(screenBounds.northeast, screenDiameterInMeters[0], 25d)); builder.include(SphericalUtil.computeOffset(screenBounds.southwest, screenDiameterInMeters[0], 215d));
But somehow it doesn't work when I'm above the equator?
Greetings and thanks for the great library!
I think it had to do something with too long distances. When i keep them about 2.000.000 meters it works!
@broady are you going to include something like this in the clustermanager/alghorithm?
Thank you for the library!
I'd also like to suggest that the Algorithm interface include a cancel() member, and that ClusterManager.cluster() call it to stop any previous expensive clustering operations. It makes a difference when the user makes multiple rapid camera changes, e.g. zooming or panning. Better than relying on Java to kill the ClusterTask background thread IMO.
Is there any practicable solution for this issue yet? Edit: gruelfin's solution with the AsyncTask helped me a lot. Although, it still is imperative to keep track of one big list of markers over which you have to iterate. Im my case, I have some built in filters, the user can set, in order to minimize the number of total markers. Every time such a setting is changed, I have to update the listOfMarkers. But it is a small nuisance compared to having to recluster this complete list.
Any word on this? I have about 8k markers and the lib is unusable at that point unfortunately. And I say unfortunately with great respect as this is a beautiful lib as far as UI, very easy to use and documented well. Really hoping I can use this soon :(
At a high level zoom things are pretty good. However as I start to zoom in performance degrades. I assume this is due to markers and clusters off the viewport being drawn/redrawn as I zoom.
Then once I am zoomed all the way in if I zoom out quickly I notice a huge hit. All the markers are unclustered (as I was all the way zoomed in before). I keep zooming out until I can see the entire map. I can see all (unclustered) markers and it takes 20-30 seconds to loop through all 8K markers and place them back into a clusters.
All of this probably relates to the fact that off screen markers and clusters are being drawn. However I also wonder if this is slow due to the animations. In cases like this it might make sense to be able to turn off animations and just remove and redraw all clusters and marker on zoom change.
I made the following changes locally to get the library to work reasonably well with lots of markers:
- Disable animation completely. There is a constant you can change in the existing code for this.
- When camera motion stops, use an AsyncTask to remove all items from the ClusterManager, and then re-add only those visible on the screen. (I do this only if there's a large number of markers.)
- In ClusterManager, I imported the mClusterTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR) change from upstream (I had an older version of the library).
- I added a cancel() method on Algorithm, and a periodic check in NonHierarchicalDistanceBasedAlgorithm.getClusters, so that I could cancel previous clustering operations when starting a new one.
I like this library but I wish the team were more responsive to obvious problems like this and https://code.google.com/p/gmaps-api-issues/issues/detail?id=5313
On Mon, Apr 6, 2015 at 6:44 PM, Billy Newman [email protected] wrote:
Any word on this? I have about 8k markers and the lib is unusable at that point unfortunately. And I say unfortunately with great respect as this is a beautiful lib as far as UI, very easy to use and documented well. Really hoping I can use this soon :(
At a high level zoom things are pretty good. However as I start to zoom in performance degrades. I assume this is due to markers and clusters off the viewport being drawn/redrawn as I zoom.
Then once I am zoomed all the way in if I zoom out quickly I notice a huge hit. All the markers are unclustered (as I was all the way zoomed in before). I keep zooming out until I can see the entire map. I can see all (unclustered) markers and it takes 20-30 seconds to loop through all 8K markers and place them back into a clusters.
All of this probably relates to the fact that off screen markers and clusters are being drawn. However I also wonder if this is slow due to the animations. In cases like this it might make sense to be able to turn off animations and just remove and redraw all clusters and marker on zoom change.
— Reply to this email directly or view it on GitHub https://github.com/googlemaps/android-maps-utils/issues/29#issuecomment-90308332 .
@akirmse agreed, wish issues like this would get more attention. Of course I am not contributing so I can't really complain ;)
Is this being actively worked by contributors, or has anyone forked, maybe a possible PR for this?
For more than two years the clustering element of this library is totally unusable. Clustering more than 100 items even on a big area brings huge performance issues. This should be either adressed or removed completely from the library. The suggestion by @akirmse does significantly improve the performance of this library.
Heres my hacked version : Update NonHierarchicalDistanceBasedAlgorithm getClusters function to
public Set<? extends Cluster
> getClusters(double zoom, LatLngBounds bounds)
Add the following if to the QuadItem
if(!bounds.contains(candidate.mPosition)) { continue; }
Replace ClusterManager.cs onCameraChange function with the following:
mMap.clear(); mPreviousCameraPosition = mMap.getCameraPosition(); currentBounds = mMap.getProjection().getVisibleRegion().latLngBounds; Set<? extends Cluster
> results = mAlgorithm.getClusters(mPreviousCameraPosition.zoom, currentBounds); for(Cluster cluster : results) { // Strip out DefaultClusterRendered.cs perform method and render each cluster accordingly here }
@akirmse can you show your example in code?