微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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 被清除时,标记列表也会被清除

提前致谢

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。