简体   繁体   English

在Android地图中绘制多边形

[英]Draw a polygon in Android Map

I would like to have ONLY the circle and not every line to every point of the circle... you have have further explanations looking at the image: 我只希望有一个圆圈,而不是每个直线都指向该圆圈的每个点...您对图像有进一步的解释:

在此处输入图片说明

This is the code that I'm using right now: 这是我现在正在使用的代码:

    final View rootView = layoutInflater.inflate(R.layout.fragment_new_alert, paramViewGroup, false);
    FrameLayout fram_map = (FrameLayout) rootView.findViewById(R.id.fram_map);
    Button btn_draw_State = (Button) rootView.findViewById(R.id.btn_draw_State);
    isMapMoveable = false; // to detect map is movable

    SupportMapFragment customMapFragment = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map));
    mMap = customMapFragment.getMap();

    btn_draw_State.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            isMapMoveable = !isMapMoveable;
        }
    });

    fram_map.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            float x = event.getX();
            float y = event.getY();

            int x_co = Math.round(x);
            int y_co = Math.round(y);

            Point x_y_points = new Point(x_co, y_co);

            LatLng latLng = mMap.getProjection().fromScreenLocation(x_y_points);

            if (!isMapMoveable) {
                return false;
            }

            int eventaction = event.getAction();
            switch (eventaction) {
                case MotionEvent.ACTION_DOWN:
                    // finger touches the screen
                    if (polyline != null){
                        polyline.setZIndex(0);
                        polyline.remove();
                        val.removeAll(polyline.getPoints());
                    }
                    val.add(latLng);
                case MotionEvent.ACTION_MOVE:
                    // finger moves on the screen
                    val.add(latLng);
                case MotionEvent.ACTION_UP:
                    // finger leaves the screen
                    drawMap();
                    break;
            }
            return true;
        }
    });
    return rootView;
}

public void drawMap() {
    rectOptions = new PolylineOptions();
    rectOptions.addAll(val);
    rectOptions.color(Color.RED);
    rectOptions.width(7);
    polyline = mMap.addPolyline(rectOptions);
}

Based on the code found here: How to draw free hand polygon in Google map V2 in Android? 基于以下代码: 如何在Android的Google Map V2中绘制自由手多边形?

Any idea? 任何想法? :) thanks! :) 谢谢!

    fram_map.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    float x = event.getX();
                    float y = event.getY();

                    int x_co = Math.round(x);
                    int y_co = Math.round(y);

                    projection = mGoogleMap.getProjection();
                    Point x_y_points = new Point(x_co, y_co);

                    LatLng latLng = mGoogleMap.getProjection().fromScreenLocation(x_y_points);
                    latitude = latLng.latitude;

                    longitude = latLng.longitude;

                    int eventaction = event.getAction();
                    switch (eventaction) {
                        case MotionEvent.ACTION_DOWN:
                            // finger touches the screen

                            if (mMarkerPoints != null) {
                                mMarkerPoints = new ArrayList<>();
                            }
                            mGoogleMap.clear();
                            mMarkerPoints.add(new LatLng(latitude, longitude));

                        case MotionEvent.ACTION_MOVE:
                            // finger moves on the screen
                            mMarkerPoints.add(new LatLng(latitude, longitude));
                            Draw_Map();
                            break;

                        case MotionEvent.ACTION_UP:
                            // finger leaves the screen
                            Draw_Map_polygon();
                            break;
                    }

                    if (Is_MAP_Moveable == true) {
                        return true;
                    } else {
                        return false;
                    }

                }
            });

//----------- for Making Polygone

    public void Draw_Map_polygon() {
            rectOptions = new PolygonOptions();
            rectOptions.addAll(mMarkerPoints);
            rectOptions.strokeColor(Color.RED);
            rectOptions.strokeWidth(5);
            rectOptions.fillColor(Color.parseColor("#40c4c4c4"));
            polygon = mGoogleMap.addPolygon(rectOptions);
            mGoogleMap.getUiSettings().setAllGesturesEnabled(true);

        }

//----------- When Moving your Hand


        public void Draw_Map() {
            mGoogleMap.addPolyline(new PolylineOptions()
                    .addAll(mMarkerPoints)
                    .width(5)
                    .color(Color.RED));
        }

问题是由于缺少开关中断

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

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