android maps asynchronous loading of overlay items

杀马特。学长 韩版系。学妹 提交于 2019-12-02 19:50:56

Use AsyncTask to load the individual layers per screen. Get the Lat/Long of the currently visible map using MapView api. On the backend send them the lat/long bounding box to get the items you want. So roughly from memory it would be something like:

public class LoadMapItems extends AsyncTask<Integer,Integer,List<ItemizedOverlay>> {
   private MapView view;

   public LoadMapItems( MapView view ) {
      this.view = view;
   }

   public List<ItemizedOverlay> doInBackground( Integer... params ) {
      int left = params[0];
      int top = params[1];
      int right = params[2];
      int bottom = params[3];

      return convertToItemizedOverlay( someService.loadSomething( left, top, right, bottom ) );
   } 

   private List<ItemizedOverlay> convertToItemizedOverlay( List<SomeObject> objects ) {
      // ... fill this out to convert your back end object to overlay items
   }

   public void onPostExecute(List<ItemizedOverlay> items) {
      List<Overlay> mapOverlays = mapView.getOverlays();
      for( ItemizedOverlay item : items ) {
         mapOverlays.add( item );
      }
   }
}

// somewhere else in your code you do this:

GeoPoint center = someMap.getMapCenter();
new LoadMapItems( someMap ).execute( 
      center.getLongitude() - someMap.getLongitudeSpan() / 2,
      center.getLatitude() - someMap.getLatitudeSpan() / 2,
      center.getLongitude() + someMap.getLongitudeSpan() / 2,
      center.getLatitude() + someMap.getLatitudeSpan() / 2);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!