Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid google maps v2 lag when markers are being drawn/updated
    text
    copied!<p>What is the proper way to manage Markers in Google Maps API v2? I'm trying to update the viewport with only markers that are visible within that region and I'm using <code>OnCameraChangeListener</code> to know when to update the viewport with the latest markers. However, it looks like the map is not scrollable/zoomable whenever it is drawing or updating the markers. I've noticed that when <code>setIcon()</code> is being used, the lag is even worse. In Google Maps v1, I never had this trouble when I used ItemizedOverlays. Here the gist of my code that is experiencing the lag in the scrolling/zooming:</p> <pre><code>private void updatePinsOnMap(MapDisplayData mapDisplayData) { BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(getPinWithBadgeCount(2)); List&lt;LatLng&gt; latlngs = Lists.newArrayList(); for (NearbyPlaceEdge place : mapDisplayData.getPlaces()) { latlngs.add(place.latlng); } for (NearbyPlaceCluster cluster : mapDisplayData.getClusters()) { latlngs.add(cluster.latlng); } int numMarkersToPlot = latlngs.size(); for (int i = 0; i &lt; numMarkersToPlot; i++) { if (i &lt; mMarkers.size()) { // recycle mMarkers.get(i).setPosition(latlngs.get(i)); } else { final Marker m = mGoogleMap.addMarker( new MarkerOptions() .position(latlngs.get(i)) .icon(bitmapDescriptor) ); mMarkers.add(m); } } for (int i = mMarkers.size() - 1; i &gt;= numMarkersToPlot; i--) { mMarkers.remove(i).remove(); } } </code></pre> <p>UPDATE:</p> <p>I created a sample app with the above code and things work according to plan (i.e. no lagginess). I spent some time thinking about this and I believe it has something to do with the memory that is required for Google Maps v2. From looking at logcat, every time I move the map--which triggers the map update--I get a lot of gc messages like this on my Nexus 4</p> <pre><code>05-27 14:28:23.701: D/dalvikvm(2849): GC_FOR_ALLOC freed 1198K, 23% free 26889K/34504K, paused 112ms, total 112ms </code></pre> <p>Is this a potential memory leak within the API? My existing app already takes up some memory, but I figure that GMaps will be able to appropriately manage its own memory based on what is available. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload