Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Google Map addMarker() very slow when adding 400 markers

Thank you for taking the time to read this.

I published an app to the Google Play store about a month ago(08/29/14) and this wasn't a problem with the same amount of markers. This week, i've noticed when i went onto my app that loading the 400 markers to the map took about 10-20 seconds on my Galaxy S5 and that there is a small circle in the middle of the default map marker icons. Before this, the map loaded in less than a second. I haven't updated or changed my app. I'm guessing Google updated the Google Map API and the change made it much slower to load markers? Has this happened to anyone else? Does anyone else have information about this?

My app is available for free on the Play Store. You can search NYS Canal Guide to find it if you want to see how slow it loads.

This method gets called in the onCreateView() of the map fragment after the map is initialized:

    private void addExistingMarkersToMap(){
    log("Adding existing markers to the map. poiAdapter size = " + poiAdapter.getCount());
    Marker marker;
    MarkerOptions markerOptions;

    for(MapMarker mapMarker : poiAdapter){
        if(markersNotFilteredOut(mapMarker)){
            markerOptions = mapMarker.getMarkerOptions();

            if(markerOptions != null && mapMarker != null){
                marker = mMap.addMarker(markerOptions);
                mapMarker.setMarker(marker);
            }
        }
    }
}

This is the getMarkerOptions() method on a MapMarker class:

public MarkerOptions getMarkerOptions() {
    return new MarkerOptions()
    .title(name)
    .position(new LatLng(lat, lng))
    .snippet(bodyOfWater + ", mile " + mile)
    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
}

The full source code is here: github.com/olearyj234/NYS-Canal-Guide

EDIT:

** To be specific, the only part that is slow is loading the markers when the map is being initialized(so also when switching from one navigation tab and back to the map tab). It isn't slow when moving the map or anything else.

Here are some logs that come up:

While the screen is frozen for a few seconds(5-15) because its loading the markers, this log is being produced very fast. The bitmap id continues to increment by one and while the markers are loading, it produces about 400 of these logs. This shows that it has to be a problem related to loading the marker bitmaps on the screen. I'm using the default marker bitmap.

   10-01 15:45:07.222: D/skia(32108): GFXPNG PNG bitmap created width:16 height:32 bitmap id is 414

This log is produced when the app is finished loading all the markers to the map.

10-01 15:51:09.402: I/Choreographer(8353): Skipped 1130 frames!  The application may be doing too much work on its main thread.

This log is produced 14 times when the app is finished loading all the markers to the map.

10-01 15:59:13.882: I/dalvikvm-heap(8353): Grow heap (frag case) to 40.668MB for 4194320-byte allocation

EDIT 2:

I just checked to see how long it would take for certain lines of code. I used System.currentTimeMillis(); in order to get times. In the method addExistingMarkersToMap(), this is the line that took long: marker = mMap.addMarker(markerOptions); When adding all 400 markers, it took 54 ms on average for each marker. The min time was 34ms and the max was 114ms.

If there's any more information you think i should provide, let me know in a comment. Thank you!

like image 913
James Avatar asked Sep 30 '14 00:09

James


People also ask

Why is Google Maps so slow on my Android?

Clear app data Google Maps stores data like shared locations, saved locations, and map tiles on your phone or tablet. Clearing this data will do the following things: Delete cache (including search suggestions, direction searches, map tiles, and activity page content stored on your device) Delete cookies.

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.

How many markers can you add to Google Maps?

Add a place On your computer, sign in to My Maps. Open or create a map. A map can have up to 10,000 lines, shapes, or places.


1 Answers

This appears to be a new problem introduced in Google Maps API v2 (looks like one of the Play Services 6 updates), see #7174 for more info (and please star it).

From the info provided in the issue, the problem seems to be specific to using a default marker with a hue, e.g.:

BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)

There are some workarounds. Easiest is to not provide a hue (if all your markers being red is acceptable):

BitmapDescriptorFactory.defaultMarker()

Or use custom drawables:

BitmapDescriptorFactory.fromResource(R.drawable.map_marker)

I see a ~2000 times slowdown when using the default marker with a hue. I'm going with the custom drawables as a solution for now.

like image 186
myanimal Avatar answered Oct 24 '22 03:10

myanimal