开发者

Android google maps groundoverlay

开发者 https://www.devze.com 2023-04-09 11:40 出处:网络
In the google maps javascript api you can add a groundoverlay and just pass two points, the point lefttoplat&lefttoplong and the point rightbottomlat&rightbottomlong... (The normal overlays do

In the google maps javascript api you can add a groundoverlay and just pass two points, the point lefttoplat&lefttoplong and the point rightbottomlat&rightbottomlong... (The normal overlays don't stay between these two points, but stay the same size when you zoom in or out.)

I have been searching for something like that in the Android google maps api, but I can't find it anywhere. Is there such a thing in the android maps api and if there's not, what's the best way to simulate开发者_开发问答 this function?


This is better. Using the draw-method makes sure you only draw the part of the bitmap that is actually on the screen. I still get the exceeded VM-budget error sometimes. I'm still looking into that.

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();
    }
}


Fixed it myself.

For everyone with the same problem, here is my code:

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;
    }

}

You'll have to do the setMarker(drawable) somewhere and it should work. I'm doing that somewhere else (hence the public drawable).

0

精彩评论

暂无评论...
验证码 换一张
取 消