在谷歌地图中添加新折线时如何删除以前添加的折线

时间:2021-05-25 17:04:21

标签: android google-maps google-maps-markers google-polyline polylineoptions

mapFragment.getMapAsync { GoogleMap ->

        gMap = GoogleMap
        val sensor = "false"
        val mode = "driving"
        val routeList = ArrayList<LatLng>()
        var distance : String? = null

        gMap.setOnMarkerClickListener { marker ->
            val title = marker.title
            if (title.equals(resources.getString(R.string.your_location))){
                Toast.makeText(requireContext(), resources.getString(R.string.your_location), Toast.LENGTH_SHORT).show()
            } else {
                try {
                    val originLatitude = latitude
                    val originalLongitude = longitude
                    val destinationLatitude = marker.position.latitude
                    val destinationLongitude = marker.position.longitude

                    val retrofitClient = RetrofitClient.apiInterface.getJson("$originLatitude,$originalLongitude",
                        "$destinationLatitude,$destinationLongitude",sensor, mode, "Api_KEY")
                    retrofitClient.enqueue(object : Callback<DirectionResults>{
                        override fun onResponse(
                            call: Call<DirectionResults>,
                            response: Response<DirectionResults>
                        ) {
                            if (response.body()!!.routes.isNotEmpty()){
                                var decodeList = ArrayList<LatLng>()
                                val route = response.body()!!.routes[0]
                                if (route.legs.isNotEmpty()){
                                    distance = route.legs[0].distance.text
                                    val steps = route.legs[0].steps
                                    var step : Steps
                                    var polyLine : String
                                    var location : com.example.parkingapp.model.marker.Location
                                    for (element in steps){
                                        step = element
                                        location = step.start_location
                                        routeList.add(LatLng(location.lat, location.lng))
                                        polyLine = step.polyLine.points
                                        decodeList = RouteDecode.decodePoly(polyLine)
                                        routeList.addAll(decodeList)
                                        location = step.end_location
                                        routeList.add(LatLng(location.lat, location.lng))
                                    }
                                }
                            }

                            if (routeList.size > 0){
                                val rectLine = PolylineOptions().width(6f).color(Color.RED)

                                for (i in 0 until routeList.size){
                                    rectLine.add(routeList[i])
                                }
                                gMap.addPolyline(rectLine)
                                MarkerOptions().position(LatLng(destinationLatitude, destinationLongitude))
                            }

                            Toast.makeText(requireContext(), distance, Toast.LENGTH_LONG).show()
                        }

                        override fun onFailure(call: Call<DirectionResults>, t: Throwable) {
                            Toast.makeText(requireContext(), t.localizedMessage, Toast.LENGTH_LONG).show()
                        }

                    })
                }catch (e : NullPointerException){
                    e.printStackTrace()
                }
            }
            false
        }
    }

我的地图中有标记列表。当我单击任何标记时,它会在该标记与我当前位置之间绘制一条路线。我的问题是当我单击另一个标记时,先前的路线折线保留在地图中。我想删除它。 注意:不要使用 GoogleMap.clear() 因为当 GoogleMap 被清除时,标记列表也会被清除

提前致谢

0 个答案:

没有答案