android maps asynchronous loading of overlay items

前端 未结 1 1272
别那么骄傲
别那么骄傲 2021-02-02 04:54

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 asynchron

1条回答
  •  执念已碎
    2021-02-02 04:57

    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> {
       private MapView view;
    
       public LoadMapItems( MapView view ) {
          this.view = view;
       }
    
       public List 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 convertToItemizedOverlay( List objects ) {
          // ... fill this out to convert your back end object to overlay items
       }
    
       public void onPostExecute(List items) {
          List 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);
    

    0 讨论(0)
提交回复
热议问题