简体   繁体   中英

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?

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.

Bobbake4 is correct. Android requires addPolygon to be on the main thread. I also have a similar issue. Currently I am implementing asynctask but there doesn't seem to be a way to perform addPolygon from doInBackground my current code is below:

    private class AddPolygonToMap extends
        AsyncTask<String, Integer, ArrayList<PolygonOptions>> {

    @Override
    protected ArrayList<PolygonOptions> doInBackground(String... urls) {
        publishProgress(1);
        return drawPolygonWithPage(urls[0]);
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        int value = progress[0] + progressBar.getProgress();
        progressBar.setProgress(value);
    }

    @Override
    protected void onPostExecute(ArrayList<PolygonOptions> result) {
        for (int i = 0; i < result.size(); i++) {
            map.addPolygon(result.get(i));
        }
    }
}

Ideally I would like addPolygon background... Is there any other way of getting this done?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM