0

私の問題は、より高いレベルにズームすると、描画された領域が消えることです。

トピックのフォローアップになります: ズームインするとAndroid MapViewオーバーレイが消える

上記のリンクは私の問題を解決して線を引きますが、塗りつぶされた領域は同じ問題のままです。

塗りつぶされた領域も解決するのに助けが必要です。

次のような私の現在のコード:

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

    }


}
4

2 に答える 2

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

    }

}
于 2012-11-30T19:28:52.420 に答える
0

Overlay の代わりに ItemizedOverlay を拡張する必要があります。

「パート 2: オーバーレイ項目の追加」で説明されているように実装します。

そして、描画メソッドとその他の必要なものをそれに追加します。その後、動作するはずです。

于 2012-11-30T19:21:54.407 に答える