Android地图的覆盖项异步加载加载、地图、Android

2023-09-05 00:06:29 作者:Doubts [疑]

我有,我想上加载项千元地图视图。 显然在创建视图的所有时,我无法加载它们。

I have a map view with thousand of items that i want to load on it. obviously i can't load them all when the view is created.

我想,我必须根据当前显示的内容异步加载它们。

I guess that I have to load them asynchronously according to what is currently displayed..

我怎么能只加载位于屏幕上显示的地图部分的项目?

How can I load only items located in the map portion displayed on the screen?

推荐答案

使用的AsyncTask加载每个屏幕各层。获取使用MapView类API当前可见的地图的纬度/长。在后台向他们发送纬度/长边框,以获得您想要的物品。因此,大致从内存中它会是这样的:

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);