Android Map GroundOverlay Example Code

Posted: January 22, 2012 in Android Dev
I am researching about "Offline map with custom tiles image" on android application.
These are some sample code about add ground overlay image as in Java Script API do.
========================================================
public class GroundOverlay extends OverlayItem {

    private MapView map;
    private Bitmap overlay;
    private GeoPoint topGeoPoint;
    private GeoPoint bottomGeoPoint;

    public Drawable drawable;

    public GroundOverlay(
            MapView map, 
            GeoPoint topLeftGeoPoint, 
            GeoPoint bottomRightGeoPoint, 
            Bitmap overlay,
            String title, 
            String snippet) {
        super(bottomRightGeoPoint, title, snippet);

        this.map = map;
        this.overlay = overlay;
        this.topGeoPoint = topLeftGeoPoint;
        this.bottomGeoPoint = bottomRightGeoPoint;

        _init();
    }

    private void _init() {
        Point topPoint = new Point();
        Point bottomPoint = new Point();

        map.getProjection().toPixels(topGeoPoint, topPoint);
        map.getProjection().toPixels(bottomGeoPoint, bottomPoint);

        int width = bottomPoint.x - topPoint.x;
        int height = bottomPoint.y - topPoint.y;

        drawable = overlayDrawable(overlay, width, height);
    }

    public BitmapDrawable overlayDrawable(Bitmap bitmap, int newWidth, int newHeight) {
        Matrix scale = new Matrix();

        float scaleFloat = (float)newWidth / (float)bitmap.getWidth();

        scale.postScale(scaleFloat, scaleFloat);
        Bitmap bitmapScaled = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), scale, false);

        BitmapDrawable bm = new BitmapDrawable(bitmapScaled);
        bm.setBounds(0,0,bitmapScaled.getWidth(),bitmapScaled.getHeight());

        return bm;
    }

}


===============================================================


public class GroundOverlay extends Overlay {

Bitmap original;
Bitmap resized;
int lastZoomlevel;
SnowmapsOverlayObject snowmapsObject;
public static Boolean DRAW = false;
GeoPoint topGeoPoint;
GeoPoint bottomGeoPoint;

OnReady _onReady = null;

Boolean ready = false;

public GroundOverlay(Bitmap original, SnowmapsOverlayObject snowmapsObject) {
    this.original = Bitmap.createScaledBitmap(original, original.getWidth(), original.getHeight(), true);
    this.snowmapsObject = snowmapsObject;

    topGeoPoint = new GeoPoint((int) ((snowmapsObject.topLat).doubleValue() * 1E6), (int) ((snowmapsObject.topLng).doubleValue() * 1E6));
    bottomGeoPoint = new GeoPoint((int) ((snowmapsObject.bottomLat).doubleValue() * 1E6), (int) ((snowmapsObject.bottomLng).doubleValue() * 1E6));
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        super.draw(canvas, mapView, false);

        Projection projection = mapView.getProjection();

        Point leftTop = new Point();
        Point rightTop = new Point();
        Point rightBottom = new Point();
        Point leftBottom = new Point();

        projection.toPixels(topGeoPoint, leftTop);
        projection.toPixels(new GeoPoint(topGeoPoint.getLatitudeE6(), bottomGeoPoint.getLongitudeE6()), rightTop);
        projection.toPixels(bottomGeoPoint, rightBottom);
        projection.toPixels(new GeoPoint(bottomGeoPoint.getLatitudeE6(), topGeoPoint.getLongitudeE6()), leftBottom);

        if (
                (leftTop.x < 0 || leftTop.y < 0) && 
                (rightTop.x < 0 || rightTop.y < 0) && 
                (rightBottom.x < 0 || rightBottom.y < 0) && 
                (leftBottom.x < 0 || leftBottom.y < 0)) {
            // Not on screen? Don't draw the overlay
            return;
        }

        //      GeoPoint mapCenter = mapView.getMapCenter();
        Paint paint = new Paint();
        paint.setFilterBitmap(true);
        paint.setAntiAlias(true);

        canvas.drawBitmap(original, null, new Rect(leftTop.x, leftTop.y, rightBottom.x, rightBottom.y), paint);

        if (!ready && _onReady != null) {
            ready = true;
            _onReady.done();
        }
}


@Override
public boolean onTouchEvent(MotionEvent e, MapView mapView) {
    super.onTouchEvent(e, mapView);

    if (e.getAction() == MotionEvent.ACTION_UP) {
        setDrawTrue();
    }

    return false;
}

private void setDrawTrue() {
    DRAW = true;
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            DRAW = false;
        }
    }, 100);
}

public void setOnReady(OnReady onReady) {
    this._onReady = onReady;
}

public interface OnReady {
    public void done();
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s