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?
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);
来源:https://stackoverflow.com/questions/5545996/android-maps-asynchronous-loading-of-overlay-items