Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Too many (potentially 10,000+) drawn polygons lagging google maps

Tags:

In my app at the moment, a 'grid' is drawn onto the map by drawing each polygon square one by one, and filling it with a color. I then add the polygon object to a map to modify later if I need to.

I only create each polygon once, however there is a major consistent 'lag' and low frame rate with the more I add (tens of thousands potentially), eventually breaking the app.

Here is the current part of code that creates and adds each 'Square' to a hashmap.

double latScale = 0.000180;
double lngScale = 0.000288;

PolygonOptions po = new PolygonOptions()
        .add(   new LatLng(lat, lng),
                new LatLng(lat, lng + lngScale),
                new LatLng(lat - latScale, lng + lngScale),
                new LatLng(lat - latScale, lng))
        .strokeWidth(3f)
        .strokeColor(Color.argb(220, 0, 0, 0));

if (colors.containsKey(color)){
    po.fillColor(colors.get(color));
}
else{
    po.fillColor(colors.get("default"));
}

gridSquares.put(square_id, mMap.addPolygon(po));

My question is, what exactly is causing it to lag so much (I realize it must be the sheer amount, but I need specifics, re drawing every frame? memory?) And how could I fulfill my need for this giant grid overlay in some way which doesn't lag so much. Either with my current line of thinking or a new way.

If any more clarification is needed please ask.

like image 688
Sam Avatar asked Jan 31 '17 17:01

Sam


1 Answers

What I ended up doing which worked really well was

  1. Using GroundOverlay in google maps for each polygon I needed, using a small resolution image (30x30, however if you only need one color this could be 1x1) for each polygon instead of drawing each. This improved performance a ton.

  2. Call OnCameraIdleListener to set each GroundOverlay to either visible or invisible depending on whether it was in view of the screen at that point. I ended up using a small buffer zone around the screen to make overlays visible in this area also.

like image 160
Sam Avatar answered Sep 24 '22 10:09

Sam