osmdroid polygon 拖动修改

循环polygon中的点,每个点增加一个marker,最后一个点除外,因为最后一个点与第一个点重复,给每个marker 做一个拖动事件,实时改变对应polygon中的坐标,其实并没有找到实时改变的方法,这里用新的polygon替换掉原有polygon的方法实现坐标的调整
for (j in 0 until listListPoint.get(i).size - 1) {
                val startkMarker = Marker(mapView)
                startkMarker.position = listListPoint.get(i).get(j)
                startkMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_CENTER)
                var drawable: Drawable =
                    ContextCompat.getDrawable(view.context, R.drawable.baseline_circle_24)!!

                startkMarker.icon = drawable
                startkMarker.title = (i.toString() + "_" + j)
                mapView.overlays.add(startkMarker)
                listListMarker[i].add(startkMarker)
                startkMarker.isDraggable = true

                startkMarker.setOnMarkerDragListener(
                    object : OnMarkerDragListener {
                        override fun onMarkerDragStart(marker: Marker) {}
                        override fun onMarkerDragEnd(marker: Marker) {
                        }

                        override fun onMarkerDrag(marker: Marker) {
                            var temp = marker.title.toString().split('_')
                            var i = temp[0].toInt()
                            var j = temp[1].toInt()
                            var index = mapView.overlays.indexOf(listPolygon[i])
                            var points = listPolygon[i].actualPoints
                            points[j].latitude = marker.position.latitude
                            points[j].longitude = marker.position.longitude
                            if (j == 0) {
                                points[points.size - 1].latitude = marker.position.latitude
                                points[points.size - 1].longitude = marker.position.longitude
                            }
                            var polygon = Polygon()
                            polygon.setStrokeWidth(1F)
                            polygon.setFillColor(view.resources.getColor(R.color.osmdroid_polygon_fill))
                            polygon.setStrokeColor(view.resources.getColor(R.color.osmdroid_polygon_stroke))
                            polygon.setPoints(points)
                            polygon.isEnabled = true
                            mapView.overlays[index] = polygon
                            listPolygon[i] = polygon

                            MyLog.d(marker.position.latitude.toString() + "," + marker.position.longitude)
                        }
                    }
                )
            }
 
 

你可能感兴趣的:(android,osmdroid)