移除多边形无效!即使我使用了polygon.remove()。如果我在多边形上按多次,它不会被删除

时间:2018-06-28 12:07:46

标签: java android kotlin onclicklistener

我在这里尝试在地图上使用多种形状,如果我单击多边形按钮,多边形将被绘制,然后单击多义线,则多边形将被删除,多边形将被绘制。与圈子相同。

我尝试使用直接可见的方法,但是为此,我需要先全局声明变量,这将导致我进入kotlinNullPointer,或者如果我使用lateinit,它将显示尚未初始化。

代码如下所示->

btnShowPolygon.setOnClickListener{
            if(polylineCount!=0 && polylineCount % 2 == 1){
                polylineCount = 0
                polyline!!.remove()
            }else if(circleCount!=0 && circleCount % 2 == 1){
                circleCount = 0
                circle!!.remove()
            }else{
                Toast.makeText(this, "Good to Go", Toast.LENGTH_SHORT).show()
                polygonCount = 0
            }
            showPolygon(listOfLocations)
        }
        btnPolyline.setOnClickListener {
            if(polygonCount!=0 && polygonCount % 2 == 1){
                polygonCount = 0
                polygon!!.remove()
            }else if(circleCount!=0 && circleCount % 2 == 1){
                circleCount = 0
                circle!!.remove()
            }else{
                Toast.makeText(this, "Good to Go", Toast.LENGTH_SHORT).show()
                polylineCount = 0
            }
            showPolyline(listOfLocations)
        }
        btnCircle.setOnClickListener {
            if(polylineCount!=0 && polylineCount % 2 == 1){
                polylineCount = 0
                polyline!!.remove()
            }else if(polygonCount!=0 && polygonCount % 2 == 1){
                polygonCount = 0
                polygon!!.remove()
            }else{
                Toast.makeText(this, "Good to Go", Toast.LENGTH_SHORT).show()
                circleCount = 0
            }
            showCircle(listOfLocations)
        }

在听众之上-

下面是方法

private fun showPolygon(listOfLocation: ArrayList<LatLng>){
        var polygonOptions = PolygonOptions()
                .strokeColor(Color.RED)
                .fillColor(Color.BLUE)
        for(i in listOfLocation){
            polygonOptions.add(i)
        }
        polygon = mMap.addPolygon(polygonOptions)
        if(polygon!!.isVisible){
            polygonCount = 1
        }

    }

    private fun showPolyline(listOfLocation: ArrayList<LatLng>){

        var polylineOptions = PolylineOptions()
                .color(Color.RED)
        for(i in listOfLocation){
            polylineOptions.add(i)
        }
        polyline = mMap.addPolyline(polylineOptions)
        if (polyline!!.isVisible){
            polylineCount = 1
        }

    }

    private fun showCircle(listOfLocation: ArrayList<LatLng>){

        var circleOptions = CircleOptions()
                .fillColor(Color.CYAN)
                .center(listOfLocation[0])
                .radius(25.00)
        circle = mMap.addCircle(circleOptions)
        if(circle!!.isVisible){
            circleCount = 1
        }
    }

0 个答案:

没有答案