Ali
Ali

Reputation: 645

Display too many map markers

I'm using Google Maps API for iOS, to display a map with a huge number of markers on it (~ 1200). Actually, it takes a really long time to display all this markers.

Would you recommend a solution or a framework which allows us to display only the markers on the visible map region ?

Thanks in advance.

Upvotes: 2

Views: 2826

Answers (2)

Yaro
Yaro

Reputation: 1252

I develop an in-house app that has

  • max 2000 markers on map (400-500 per average)
  • with 16 different marker images (pin colors)

The app started out using Apple Maps in the beginning then in the last months we switched to Google Maps. UPDATE: we switched back to Apple Maps due to performance issues :)


1: The performance of google maps is drastic.

  • Very low FPS. And it gets worse linearly as you add markers on the map.
  • It does not care if the marker is visible on screen or not. You have a memory footprint. Apple maps has methods to reuse (just like UITableView has dequeueReusableCellWithIdentifier methods) a marker that is not visible on screen, saving a lot of memory and boosting screen drawing speed. All that Google maps say about performance in documentation is:

If you are creating several markers with the same image, use the same instance of UIImage for each of the markers. This will help improve the performance of your application when displaying many markers.

But that is not enough. I have tried adding 2000 (1px x 1px size, 4 color) gif images as marker.icon and the result was the same. Yes, memory footprint reduced from 60MB to 54MB BUT it was still lagging as hell when you move the camera.

Google maps is a very slim framework compared to Apple maps. I have tried everything. Apple maps can handle 2000 pins and Google maps does not.

What to definitely do when having 300+ pins on map:

  • add only visible pins on screen (this means you have to readd markers as soon as camera angle changes)
  • use clusters (I used them only if the number of visible markers on screen is higher then a predefined value [MAX_NUM_MARKER])
  • detect device type and set the predefined MAX_NUM_MARKER according to it
  • clustering is basically re-adding all the pins on map. That takes time too btw. We have pins very close to each others, we managed to keep 200-250 pins on map that way (instead of 700)
  • don't use too many different pin images. Reduce the image quality as possible. And use it as Google documentation says.

MAX_NUM_MARKER code:

int MAX_NUM_MARKER = 200;    
if (IS_IPAD) {
        if ([[[UIDevice currentDevice] platformString] isEqualToString:@"iPad 1G"]) {
            MAX_NUM_MARKER = 150;
        } else if ([[[UIDevice currentDevice] platformString] isEqualToString:@"iPad 2G"]) {
            MAX_NUM_MARKER = 200;
        } else if ([[[UIDevice currentDevice] platformString] isEqualToString:@"iPad 3G"]) {
            MAX_NUM_MARKER = 300;
        } else if ([[[UIDevice currentDevice] platformString] isEqualToString:@"iPad 4G"]) {
            MAX_NUM_MARKER = 400;
        } else {
            MAX_NUM_MARKER = 400;
        }
    } else {
        if (IS_IPHONE_6 || IS_IPHONE_6P) {
            MAX_NUM_MARKER = 400;
        } else if (IS_IPHONE_5) {
            MAX_NUM_MARKER = 300;
        } else if (IS_IPHONE_4_OR_LESS) {
            MAX_NUM_MARKER = 150;
        } else {
            MAX_NUM_MARKER = 500;
        }
    }

is marker visible on screen code:

 - (BOOL)isMarkerVisibleOnMap:(GMSMarker*)marker {

        float padding = 0.0f;

        CGPoint point = [map.projection pointForCoordinate:marker.position];

        if (point.x >= -padding && point.y >= -padding && point.x <= map.frame.size.width+padding
            && point.y <= map.frame.size.height+padding) {
            return YES;

        }

        return NO;
    }

For Clustering we used https://github.com/googlemaps/google-maps-ios-utils found on gitHUb. Well designed, easy to use and tweak (not official) library. You basically add the markers into array once and then use "clustering" anytime the pins on map needs to be displayed.

For those of you who is thinking to start Google maps because of the better satellite image quality:

Take a look at Apple iOS9 new map mode: SatelliteFlyover. Now thats quality + battery drain :D

Upvotes: 5

Verma
Verma

Reputation: 8429

Google Maps for iOS doesn't officially current support Marker Clustering as seen over here.

As seen over here, the are few work around you can do

Upvotes: 1

Related Questions