Android谷歌地图地面覆盖

时间:2011-09-29 09:35:32

标签: android google-maps overlay

在google地图javascript api中,你可以添加一个地面覆盖,然后只传递两个点,即lefttoplat& lefttoplong和point rightbottomlat& rightbottomlong ......(正常覆盖不会停留在这两个点之间,但保持放大或缩小时尺寸相同。)

我一直在Android google maps api中搜索类似的内容,但我无法在任何地方找到它。在android maps api中是否有这样的东西,如果没有,那么模拟这个函数的最佳方法是什么?

2 个答案:

答案 0 :(得分:2)

这样更好。使用draw-method确保只绘制实际位于屏幕上的位图部分。有时候我仍然会遇到超出的VM预算错误。我还在调查那个。

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

答案 1 :(得分:0)

我自己修好了。

对于遇到同样问题的每个人,这是我的代码:

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

}

你必须在某处做setMarker(drawable),它应该可以工作。我在其他地方这样做(因此公众可绘制)。