因此,我最终要做的是将标记所代表的对象添加到其标记中。然后在onMarkerClickListener中,我通过标记对象检查标记类型。像这样(kotlin):
map.setOnMarkerClickListener { marker ->
val mapEvent: MapEvent?
mapEvent =
if (marker.tag is MapEvent) {
marker.tag as MapEvent?
} else {
val clickedMarker = getClickedMarker(from = map)
if (clickedMarker != null)
clickedMarker.tag as MapEvent?
else
return@setOnMarkerClickListener false
}
markerClicked(this@MainActivity, mapEvent)
true
}
如果对象的类型为MapEvent,则单击的标记不是MapEvent的类型,我调用markerClicked()。我将浏览所有标记的列表,以查看单击的标记是否在任何MapEvent标记的半径之内。如果是这样,我使用该标记并调用markerClicked()。
这是我对标记列表进行排序的方式:
fun getClickedMarker(from: GoogleMap): Marker? {
if (!::userMarker.isInitialized)
return null
var clickWidth = ContextCompat.getDrawable(App.context, R.drawable.ic_map_permanent)!!.intrinsicWidth
clickWidth += (clickWidth / 3)
val g0 = from.projection.fromScreenLocation(Point(0, 0))
val g1 = from.projection.fromScreenLocation(Point(clickWidth, clickWidth))
val latRadius = (g1.latitude - g0.latitude) / 2
val lonRadius = (g1.longitude - g0.longitude) / 2
return try {
eventMarkers.single {
it.position!!.latitude - latRadius > userMarker.position.latitude && // west
it.position!!.latitude + latRadius < userMarker.position.latitude && // east
it.position!!.longitude + lonRadius > userMarker.position.longitude && // north
it.position!!.longitude - lonRadius < userMarker.position.longitude // south
}
} catch (e: Exception) {
null
}
}
如果不清楚,请发表评论。我希望这可以对其他人有所帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句