问题描述
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 (将#修改为@)