Question

I have activity which can show overlay. This overlay have markers which gets from web.

How can I implements lazy loading from URL for this markers?

Thx, Igor

Was it helpful?

Solution

What you need to do is get create an AsyncTask to retrieve your data. Once the async task executes and you have the data the UI thread will call onPostExecute(). In your onPostExecute() method you will add your data to your map view the same you would in the example android code.

Here's the a mostly complete example. You should be able to fill in the holes with this.

    public class SomeActivity extends MapActivity {
        private MapView mYourMapView;

        protected void onCreate(Bundle yourbundle){
          super.onCreate(yourbundle);
          setContentView(R.layout.yourcontentview);

          mYourMapView = (MapView)findViewById(R.id.yourmapviewid);

          GetYourDataTask task = new GetYourDataTask(mYourMapView);
          task.execute();
        }
     }

     public class GetYourDataTask extends AsyncTask<Void, Void, Void>{
           private MapView mView;
           private List<Items> mYourItemsFromInternetSource;

           public GetYourDataTask(MapView view){
              this.mView = view;
           }

           protected Void doInBackground(Void... params){
                .....get some data from internets
                mYourItemsfromInternetSoruce = something you got from internet;
                return null;
           }

           protected Void onPostExecute(){
                YourOverlay overlay = new YourOverlay(mYourItemsFromInterSource);
                mView.getOverlays().add(overlay);
           }
    }

    public YourOverlay extends ItemizedOverlay<OverlayItem>{
              private List<OverlayItem> mItems;
              public YourOverlay(List<Item> itemsFromInternet)[
                     super(boundCenterBottom(someContext.getResources().getDrawable(R.drawable.map_pin)));
                     //for your items create overlay items then
                     List<OverlayItem> createdItems = someConvertFunction(itemsFromInternet);
                     for(OverlayItem item: createdItems){
                          mItems.add(item);
                          populate();
                     }
               }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top