简体   繁体   English

在地图上绘制多个多边形

[英]Draw more than one Polygon on Map

I am facing this simple yet annoying problem in developing an App on AndroidStudio. 在AndroidStudio上开发应用程序时,我面临着这个简单而烦人的问题。 I need to make my app able to draw more than one polygon on map. 我需要使我的应用程序能够在地图上绘制多个多边形。

You need to know that: 1. I don't want to have a fixed number of polygon's vertex. 您需要知道:1.我不想有固定数量的多边形顶点。 2. I don't want to have a fixed number of polygons, I would like to add as many as I want. 2.我不想有固定数量的多边形,我想添加任意数量的多边形。 3. I don't know the coordinates of the polygon's vertex in advance, I want to get them by clicking on the map. 3.我事先不知道多边形顶点的坐标,我想通过单击地图来获取它们。

What I did so far: First, I wrote the OnMapClick method, which let me get the coordinates of the point I click and put them in an ArrayList (arrayPoints). 到目前为止,我所做的事情:首先,我编写了OnMapClick方法,该方法使我可以获取单击的点的坐标并将其放置在ArrayList(arrayPoints)中。 Then the other two methods (onPolylineClick and onMarkerClick) allow me to "close" the polygon and thus to create it: 然后其他两种方法(onPolylineClick和onMarkerClick)允许我“关闭”多边形并因此创建它:

 @Override
    public void onMapClick(LatLng latLng) {

        MarkerOptions marker = new MarkerOptions();
        marker.position(latLng);
        mGoogleMap.addMarker(marker);

        polylineOptions = new PolylineOptions();
        polylineOptions.clickable(true);
        polylineOptions.color(Color.BLACK);
        polylineOptions.width(8);
        arrayPoints.add(latLng);

        polylineOptions.addAll(arrayPoints);


        mGoogleMap.addPolyline(polylineOptions);


    }


   @Override
    public void onPolylineClick(Polyline polyline) {

        Polygon polygon = mGoogleMap.addPolygon(new PolygonOptions()
                .clickable(true)
                .addAll(arrayPoints));


        stylePolygon(polygon);

    }


    @Override
    public boolean onMarkerClick(Marker marker) {

        if (arrayPoints.contains(marker.getPosition())) {
            polygon = mGoogleMap.addPolygon(new PolygonOptions()
                    .clickable(true)
                    .addAll(arrayPoints));


            stylePolygon(polygon);
            polygoncoord = polygon.getPoints().toString();
            Log.i("polygoncoord", polygon.getPoints().toString());
        } else {
            AddPointOfAnalysisLabel(label, point_coords);
        }
        return true;
    }

Now the problem is that if I keep clicking on the map after the first polygon is closed, I also keep adding new marker of the same polyline, instead of creating a new polygon. 现在的问题是,如果在第一个多边形关闭后继续单击地图,我还将继续添加同一折线的新标记,而不是创建新的多边形。

My idea is that maybe I can click on the polygon to close it and then this will allows me to create a new one (without removing the first one). 我的想法是,也许我可以单击该多边形以将其关闭,然后这将允许我创建一个新的多边形(而无需删除第一个多边形)。 Therefore I already prepared the onPolygonClick method, but I really have know idea what to write in it to realize what I have in my mind... Can you please help me? 因此,我已经准备了onPolygonClick方法,但是我真的知道要在其中写些什么,以实现我的主意...您能帮我吗?

Thanks in advance! 提前致谢!

关闭多边形后,您需要重置arrayPoints列表,以便可以开始为下一个多边形收集点。

arrayPoints.clear();

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

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