简体   繁体   English

在Google地图上绘制矩形

[英]Draw rectangle on google map

I am extend a SupportMapFragment which contain google map here, I lay my google map on a frameLayout according to How to handle onTouch event for map in Google Map API v2? 我在这里扩展了一个包含Google地图的SupportMapFragment,我根据如何在Google Map API v2中处理地图的onTouch事件将Google地图放置在frameLayout上? . Ondraw()method is called , but the problem is rectangle is never been showed, even when I use setWillNotDraw(false); 调用了Ondraw()方法,但问题是,即使我使用setWillNotDraw(false);也从未显示矩形。 here. 这里。

I already got the right position of the rectangle, the only problem is it is not showed. 我已经有了矩形的正确位置,唯一的问题是它没有显示。 Following is my most part of code: 以下是我大部分的代码:

Add-on:When I remove the google map it works as I expect, actually the rectangle drawed on the background frameLayout, so it will be covered by the map, now the way I solve the question is add a image view as a rectangle and dynamic change its size when touch moving. 附加组件:当我删除谷歌地图时,它按预期工作,实际上是在背景frameLayout上绘制了矩形,因此它将被地图覆盖,现在我解决问题的方法是将图像视图添加为矩形并触摸移动时动态更改其大小。 I have no idea how to draw directly on the top of this viewgroup(framelayout) at the moment 我现在不知道如何直接在此视图组(framelayout)的顶部绘制

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = super.onCreateView(inflater, container, savedInstanceState);
    //stash reference to google map 
    mGoogleMap = getMap();
    //show the location
    mGoogleMap.setMyLocationEnabled(true);
    mTouchView = new TouchableWrapper(getActivity());
    mTouchView.addView(view);

    return mTouchView;
}

private class TouchableWrapper extends FrameLayout {

    //box is used to illustrate the drawing box, the painter is used to set the color of the box.
    private Box mCurrentBox = null;
    private Paint mBoxPaint;

    public TouchableWrapper(Context context) {
        super(context);
        mBoxPaint = new Paint();
        mBoxPaint.setColor(0xffff0000);
    //  mBoxPaint.setStrokeMiter(20);
        mBoxPaint.setStrokeWidth(8);    
        this.setWillNotDraw(false);
        setWillNotDraw(false);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // decide when the child should hold the motion event.
        return true;    //always let the relative layout to hold the motion event.
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        PointF curr = new PointF(event.getX(), event.getY());
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                 mMapIsTouched = true;
                mCurrentBox = new Box(curr);//original code for draw Rectangle.
                Log.d(TAG, "action.down box is"+mCurrentBox);
                break;

            case MotionEvent.ACTION_MOVE:
                Log.i(TAG, "action_move");
                if (mCurrentBox != null) {
                    mCurrentBox.setmCurrent(curr);
                    Log.d(TAG, "action.move box is"+mCurrentBox);
                    this.invalidate();  //TODO
                }
                break;

            case MotionEvent.ACTION_UP:
                Log.i(TAG, "action_up");
                mMapIsTouched = false;
                mCurrentBox = null;
                break;
                }
            return true;
        }

    @Override
    protected void onDraw(Canvas canvas) {
        float left = 0;
        float right = 0;
        float top = 0;
        float bottom = 0;
        if (mCurrentBox!=null) {
             left = Math.min(mCurrentBox.getmOrigin().x, mCurrentBox.getCurrent().x) ;
             right = Math.max(mCurrentBox.getmOrigin().x, mCurrentBox.getCurrent().x) ;
             top = Math.min(mCurrentBox.getmOrigin().y, mCurrentBox.getCurrent().y) ;
             bottom = Math.max(mCurrentBox.getmOrigin().y, mCurrentBox.getCurrent().y) ;
            canvas.drawRect(left, top, right, bottom, mBoxPaint);
            }
        Log.i(TAG, "value is"+left+right+top+bottom);
        }

You could wrap the Map in a FrameLayout containing also a Drawable (your rectangle). 您可以将Map包装在也包含Drawable(矩形)的FrameLayout中。 In that way, once you find where the rectangle shall be, you could hide and show it without having to redraw it. 这样,一旦找到矩形的位置,就可以隐藏和显示它,而不必重新绘制它。

You can see here for an example. 您可以在此处查看示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM