【问题标题】:Draw a polygon in Android Map在 Android 地图中绘制多边形
【发布时间】:2014-09-15 03:12:42
【问题描述】:

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

这是我现在使用的代码:

    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?

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

【问题讨论】:

  • 你得到你的poliline的边界Lat lon了吗?我需要边界纬度。

标签: android google-maps polygon


【解决方案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));
        }

【讨论】:

  • 我需要在哪里清除多边形选项列表,就像我必须清除所有内容,甚至是旧 latlong 列表?
【解决方案2】:

问题是因为开关中缺少中断

【讨论】:

  • 您可以将您的答案标记为答案,这将帮助其他有同样问题的人。
  • 是的,我知道,但我只能在 24 小时后完成,我一直在等待它! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-10
  • 1970-01-01
  • 1970-01-01
  • 2020-04-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多