在Android Map中绘制一个多边形

时间:2014-07-23 19:07:48

标签: android google-maps polygon

我想只有圆圈而不是每一条线到圆圈的每个点...你有进一步的解释看图像:

enter image description here

这是我现在正在使用的代码:

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

根据此处的代码:How to draw free hand polygon in Google map V2 in Android?

有什么想法吗? :)谢谢!

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

问题是由于交换机中缺少中断