因此,我试图创建一个遍历数组列表的setOnMarkerClickListener,并在单击标记的方向上画一条线。但是,每当单击标记时,仅会为列表和循环中生成的最后一个标记生成一行。
我想到了只添加另一个数组,并为创建的每个onclick侦听器遍历该数组,但是由于它已经处于循环中……它应该只为所单击的标记生成一行,并且仅此一行
@SuppressLint("MissingPermission")
override fun onMapReady(map: GoogleMap) {
mMap = map;
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
println("I ran");
println(cons);
LOOP PRINTING THE MARKERS
val iterator = cons.listIterator()
for (i in iterator) {
map.addMarker(
MarkerOptions()
.position(LatLng(i.lat, i.long))
.title(i.name)
)
ONCLICKLISTENER FOR EACH MARKER map.setOnMarkerClickListener(GoogleMap.OnMarkerClickListener {
val path: MutableList<List<LatLng>> = ArrayList()
URLDirections where. its supposed to take in each longitude and latitude on point
val urlDirections = "https://maps.googleapis.com/maps/api/directions/json?origin="+(latLNGOrigin.latitude) + "," + (latLNGOrigin.longitude)+"&destination="+(i.lat) + "," + (i.long)+"&key=<APIKEYHERE>"
print(urlDirections)
val directionsRequest = object : StringRequest(Request.Method.GET, urlDirections, Response.Listener<String> {
response ->
val jsonResponse = JSONObject(response)
// Get routes
// Response.ErrorListener(error("VolleyError"))
val routes = jsonResponse.getJSONArray("routes")
val legs = routes.getJSONObject(0).getJSONArray("legs")
val steps = legs.getJSONObject(0).getJSONArray("steps")
for (i in 0 until steps.length()) {
val points = steps.getJSONObject(i).getJSONObject("polyline").getString("points")
path.add(PolyUtil.decode(points))
}
for (k in 0 until path.size) {
this.mMap!!.addPolyline(PolylineOptions().addAll(path[k]).color(Color.BLUE))
}
}, Response.ErrorListener {
_ ->
}){}
val requestQueue = Volley.newRequestQueue(this)
requestQueue.add(directionsRequest)
true
});
}
locationMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0L, 0f, this);
}
我希望它做的是在单击的标记上画一条线,并在单击时显示位置的标题(由于某些原因,onclick侦听器会覆盖它,并且如果有人可以告诉我我为什么这样做最感谢)