放大时MapView叠加消失(填充区域)

时间:2012-11-30 19:13:18

标签: android android-mapview android-canvas zooming ondraw

我的问题是当我放大到更高的水平时,绘图填充区域会消失。

它将非常跟进主题: Android MapView overlay disappearing when zooming in

以上链接解决了我的问题以画线,但填充区域仍然存在同样的问题。

我也需要帮助解决填充区域。

我目前的代码如下:

class MyOverlay extends Overlay {
    private final List<GeoPoint>    points;
    private int             oldX;
    private int             oldY;

    public MyOverlay(List<GeoPoint> points) {
        this.points = points;
    }



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

        int x1 = -1;
        int y1 = -1;
        int x2 = -1;
        int y2 = -1;

        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);

        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(5);
        int size = points.size();
        for (int i = 0; i <= size; i++) {
            Point point = new Point();
            mapView.getProjection().toPixels(points.get(i % size),
                point);
            x2 = point.x;
            y2 = point.y;
            if (i > 0) {
                canvas.drawLine(x1, y1, x2, y2, paint);
            }
            x1 = x2;
            y1 = y2;
        }

                    //the following are drawing the filled area

        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.FILL);
        paint.setAlpha(60);
        Projection projection = mapView.getProjection();
        Point p1 = new Point();
        Point p2 = new Point();
        Point p3 = new Point();
        projection.toPixels(gpoint1, p1);
        projection.toPixels(gpoint2, p2);
        projection.toPixels(gpoint3, p3);
        Path path = new Path();
        path.moveTo(p1.x, p1.y);
        path.lineTo(p2.x, p2.y);
        path.lineTo(p3.x, p3.y);
        canvas.drawPath(path, paint);

    }


}

2 个答案:

答案 0 :(得分:1)

您可以从此代码中获得帮助。

public abstract class BalloonItemizedOverlay<Item> extends
        ItemizedOverlay<OverlayItem> {

    private MapView mapView;
    private BalloonOverlayView balloonView;
    private View clickRegion;
    private int viewOffset;
    final MapController mc;

    /**
     * Create a new BalloonItemizedOverlay
     * 
     * @param defaultMarker
     *            - A bounded Drawable to be drawn on the map for each item in
     *            the overlay.
     * @param mapView
     *            - The view upon which the overlay items are to be drawn.
     */
    public BalloonItemizedOverlay(Drawable defaultMarker, MapView mapView) {
        super(defaultMarker);
        this.mapView = mapView;
        viewOffset = 0;
        mc = mapView.getController();
    }

    @Override
    public void draw(Canvas c, MapView m, boolean shadow) { // for disabling the
                                                            // shadow of overlay

        super.draw(c, m, false);
    }

    /**
     * Set the horizontal distance between the marker and the bottom of the
     * information balloon. The default is 0 which works well for center bounded
     * markers. If your marker is center-bottom bounded, call this before adding
     * overlay items to ensure the balloon hovers exactly above the marker.
     * 
     * @param pixels
     *            - The padding between the center point and the bottom of the
     *            information balloon.
     */
    public void setBalloonBottomOffset(int pixels) {
        viewOffset = pixels;
    }

    /**
     * Override this method to handle a "tap" on a balloon. By default, does
     * nothing and returns false.
     * 
     * @param index
     *            - The index of the item whose balloon is tapped.
     * @return true if you handled the tap, otherwise false.
     */
    protected boolean onBalloonTap(int index) {
        Globals.mapPinpointTap = true;
        return false;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.google.android.maps.ItemizedOverlay#onTap(int)
     */
    @Override
    protected final boolean onTap(int index) {

        boolean isRecycled;
        final int thisIndex;
        GeoPoint point;
        Globals.mapPinpointTap = true;

        thisIndex = index;
        point = createItem(index).getPoint();

        if (balloonView == null) {
            balloonView = new BalloonOverlayView(mapView.getContext(),
                    viewOffset);
            clickRegion = (View) balloonView
                    .findViewById(R.id.balloon_inner_layout);
            isRecycled = false;
        } else {
            isRecycled = true;
        }

        balloonView.setVisibility(View.GONE);

        List<Overlay> mapOverlays = mapView.getOverlays();
        if (mapOverlays.size() > 1) {
            hideOtherBalloons(mapOverlays);
        }

        balloonView.setData(createItem(index));

        MapView.LayoutParams params = new MapView.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, point,
                MapView.LayoutParams.BOTTOM_CENTER);
        params.mode = MapView.LayoutParams.MODE_MAP;

        setBalloonTouchListener(thisIndex);

        balloonView.setVisibility(View.VISIBLE);

        // /
        /*
         * String url = "http://maps.google.com/maps?q=" +
         * (point.getLatitudeE6() / 1E6) + "," + (point.getLongitudeE6() / 1E6);
         */
        // String url = "http://iceapp.coeus-solutions.de/api/map?location="
        // + (point.getLatitudeE6() / 1E6) + ","
        // + (point.getLongitudeE6() / 1E6);
        // Intent browserIntent = new Intent(Intent.ACTION_VIEW,
        // Uri.parse(url));
        // balloonView.getContext().startActivity(browserIntent);
        // /

        if (isRecycled) {
            balloonView.setLayoutParams(params);
        } else {
            mapView.addView(balloonView, params);
        }

        mc.animateTo(point);

        return true;
    }

    /**
     * Sets the visibility of this overlay's balloon view to GONE.
     */
    private void hideBalloon() {
        if (balloonView != null) {
            balloonView.setVisibility(View.GONE);
        }
    }

    /**
     * Hides the balloon view for any other BalloonItemizedOverlay instances
     * that might be present on the MapView.
     * 
     * @param overlays
     *            - list of overlays (including this) on the MapView.
     */
    private void hideOtherBalloons(List<Overlay> overlays) {

        for (Overlay overlay : overlays) {
            if (overlay instanceof BalloonItemizedOverlay<?> && overlay != this) {
                ((BalloonItemizedOverlay<?>) overlay).hideBalloon();
            }
        }

    }

    /**
     * Sets the onTouchListener for the balloon being displayed, calling the
     * overridden onBalloonTap if implemented.
     * 
     * @param thisIndex
     *            - The index of the item whose balloon is tapped.
     */
    private void setBalloonTouchListener(final int thisIndex) {

        try {
            @SuppressWarnings("unused")
            Method m = this.getClass().getDeclaredMethod("onBalloonTap",
                    int.class);
            Globals.mapPinpointTap = true;

            clickRegion.setOnTouchListener(new OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event) {

                    View l = ((View) v.getParent())
                            .findViewById(R.id.balloon_main_layout);
                    Drawable d = l.getBackground();

                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        int[] states = { android.R.attr.state_pressed };
                        if (d.setState(states)) {
                            d.invalidateSelf();
                        }
                        return true;
                    } else if (event.getAction() == MotionEvent.ACTION_UP) {
                        int newStates[] = {};
                        if (d.setState(newStates)) {
                            d.invalidateSelf();
                        }
                        // call overridden method
                        onBalloonTap(thisIndex);
                        return true;
                    } else {
                        return false;
                    }

                }
            });

        } catch (SecurityException e) {
            if (Globals.SHOW_LOGS)
                Log.e("BalloonItemizedOverlay",
                        "setBalloonTouchListener reflection SecurityException");
            return;
        } catch (NoSuchMethodException e) {
            // method not overridden - do nothing
            return;
        }

    }

}

答案 1 :(得分:0)

你应该扩展ItemizedOverlay而不是Overlay。

实现类似于描述here“第2部分:添加叠加项目”

并添加您的绘制方法和其他必要的东西。然后它应该工作。