Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Google Maps v2 polygon performance issue?

I am using the Google maps v2 api for an Android application I'm building which has to draw polygons on the map. Everything works fine when the number of polygons is small, when the number is larger the map loads slowly and panning and zooming is really slow. I am using SupportMapFragment and adding polygons like this:

    for(PolygonOptions item : items) { 
            getMap().addPolygon(poly);
    }

Is there any way to improve performance for a large number of polygons?

like image 393
Bobbake4 Avatar asked Nov 13 '22 12:11

Bobbake4


1 Answers

I also want to draw many polygons on the map in the background because loading and assembling the vertices takes time. The solution that worked for me was to load the data in the AsyncTask then pass the points for each polygon as it is read back to a method in the main UI in the onProgressUpdate method.

private class AddZonesTask extends AsyncTask<Zone, Zone, Integer> {
  protected Integer doInBackground(Zone... zones) {
    for (Zone zone : zones) {
      Cursor cursor = provider.query( .... );
      List<Points> points = cursorToPointsMethod(cursor);
      zone.add(points);
      publishProgress(zone);
    }
  return zones.length;
  }

  protected void onProgressUpdate(Zone... zones) {
    drawBorder(zones[0]);
  }

  protected void onPostExecute(Integer result) { }
}

Where drawBorder in the main UI then adds them to the map object.

like image 62
user2044636 Avatar answered Nov 15 '22 06:11

user2044636