My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
LazyLoading  
Howto for lazyloading marker(OverlayItems).
Phase-Implementation
Updated Jan 7, 2010 by christop...@gmail.com

Introduction

If you want to use a lots of marker in your MapView, it makes sense to lazyload only those which are visible for the current viewport. The mapview-overlay-manager provides an easy callback-feature for this.

LazyLoadCallback

You enable lazyloading by just adding an implementation of the LazyLoadCallback to your ManagedOverlay.

This LazyLoadCallback will be invoked when the user:

  • scrolled the map
  • zooms in/out

or you call ManagedOverlay.invokeLazyLoad(long delay) by hand

public interface LazyLoadCallback {
	public List<ManagedOverlayItem> lazyload(GeoPoint topLeft, GeoPoint bottomRight, ManagedOverlay overlay) throws LazyLoadException;
}

Important Notes

  • The Callback will run in another Thread than your UI-Thread!
  • Do not add the just created ManagedOverlayItem to the ManagedOverlay! The OverlayManager will do this afterwards in the UI-Thread.
  • This includes ManagedOverlay.createItem(). Don't do it here!

Example:

managedOverlay.setLazyLoadCallback(new LazyLoadCallback() {

	@Override
	public List<ManagedOverlayItem> lazyload(GeoPoint topLeft, GeoPoint bottomRight, ManagedOverlay overlay) throws LazyLoadException
             List<ManagedOverlayItem> items = null;
             try {
                 items = httpRequestHelper.findMarker(topLeft, bottomRight);
	     } catch (Exception e) {
		  throw new LazyLoadException(e.getMessage());
	     }
             return items;	
        }
});

LazyLoadListener

You can attach a LazyLoadListener to the ManagedOverlay. Following events will be fired when lazyloading is invoked.

  • onBegin: before anything is done.
  • onSuccess: everything is done and went fine.
  • onError: a LazyLoadException has accurred in the LazyLoadCallback.

public interface LazyLoadListener {

    public void onBegin(ManagedOverlay overlay);
    public void onSuccess(ManagedOverlay overlay);
    public void onError(LazyLoadException exception, ManagedOverlay overlay);
    
}

Note: This will run in the UI-Thread. So you can make a Toast or other stuff with Views from the Activity.

LazyLoadAnimation

It makes sense to inform the user about background activities by displaying an indicator. The mapview-overlay-manager has a built-in loading animation. You only need to provide an ImageView.

ImageView imageView = (ImageView) findViewById(R.id.loader);
managedOverlay.enableLazyLoadAnimation(imageView);

You can also use a custom animation:

LazyLoadAnimation animation = managedOverlay.enableLazyLoadAnimation(imageView);
animation.setAnimationDrawable((AnimationDrawable) this.getResources().getDrawable(R.anim.animation1));

If you wanna do more freaky stuff: use the LazyLoadListener!

Remarks

MapController.animateTo(GeoPoint)

This will scroll the MapView to a new point on the map, but will not invoke lazyloading!

Nevertheless you could do this by hand:

   managedOverlay.invokeLazyLoad(long delay);
Comment by malcolmc...@gmail.com, Apr 26, 2012

I can't seem to get it working on my site... Handbags


Sign in to add a comment
Powered by Google Project Hosting