谷歌地图onMapClickListener



我正在使用聚类在地图上绘制标记。

在某些情况下,我需要防止紧密标记被聚集。

为此,我将它们分别绘制。

默认情况下,当我打开地图时,我将其设置为集群

override fun onMapReady(map: GoogleMap?) {
googleMap = map
googleMap?.uiSettings?.isMyLocationButtonEnabled = true
googleMap?.uiSettings?.isZoomControlsEnabled = true
googleMap?.uiSettings?.isCompassEnabled = true
googleMap?.let {
setupCluster(googleMap!!)
}
googleMap?.setOnMapClickListener {
if (viewModel.isClient) {
viewModel.setType(MapNewTwoViewModel.OrderType.MyOrders())
} else
viewModel.setType(MapNewTwoViewModel.OrderType.AllOrders())
}
}
private fun setupCluster(map: GoogleMap) {
clusterManager = ClusterManager(MyApplication.applicationContext(), map)
renderer = DotRenderer()
clusterManager?.renderer = renderer
map.setOnCameraIdleListener(clusterManager)
map.setOnMarkerClickListener(clusterManager)
map.setOnInfoWindowClickListener(clusterManager)
clusterManager?.setOnClusterClickListener(this)
clusterManager?.setOnClusterItemClickListener(this)
}

即markerClickLister和其他Listener由ClusterManager控制。

有时我通过Bundle提交一个订单,它不应该是可集群的

private fun setSingleOrder(orders: List<OrderX>?) {
if(!orders.isNullOrEmpty()) {
Log.d("Marker", "3")

googleMap?.setOnInfoWindowClickListener(null)
googleMap?.setOnMarkerClickListener(null)
googleMap?.setOnMarkerClickListener {
Log.d("Marker", "1")
for((id, or) in allOrders.entries){
Log.d(id, or.toString())
}
Log.d("Marker", "2")
Log.d("Marker Snippet", it.snippet.toString())
val singleOrders = listOf(allOrders[it.snippet.toString()]!!)
viewModel.setType(MapNewTwoViewModel.OrderType.SingleOrder(), singleOrders)
showBottomSheetOrders()
true
}
Log.d("Marker", "4")
val line = PolylineOptions()
allOrders[orders[0].id.toString()] = orders[0]
for (dot in orders[0].dots) {
allDots[dot.id] = DotXPosition(dot, dot.position, orders[0].isMyOrder)
val marker = MarkerOptions()
.position(dot.position)
.snippet(orders[0].id.toString())
.icon(getItemIcon(dot))
val markerMap = googleMap?.addMarker(marker)
markerMap?.let {
markerMap.snippet = orders[0].id.toString()
markersSingleOrder.add(markerMap)
}
line.add(dot.position)
}
stylePolyline(line, orders[0].isMyOrder)
val currPolyline = googleMap?.addPolyline(line)
currPolyline?.let {
allPolylines[orders[0].id.toString()] = currPolyline
}

}
}

我试图覆盖markerClickListener,但单击标记时没有发生任何事情。它似乎仍然由ClusterManager控制,由于那里没有订单,因此不会绘制订单。

告诉我如何解决这个问题,好几天我都想不出任何

我用这种方法解决了这个问题。

在ClusterManager中,我添加了所有的点,但当我不需要对它们进行集群时,我只需将shouldRenderAsCluster中的条件更改为不可能的

override fun shouldRenderAsCluster(cluster: Cluster<DotX>): Boolean {
return cluster.size > COUNT_POINT_FOR_RENDER_IN_CLUSTER
}
viewModel.mediatorLiveData.observe(viewLifecycleOwner, {
Log.d("MediatorLiveData size", it.size.toString())
if(viewModel.isSingleOrders)
COUNT_POINT_FOR_RENDER_IN_CLUSTER = Int.MAX_VALUE
else
COUNT_POINT_FOR_RENDER_IN_CLUSTER = 1
setItemsCluster(it)
})

相关内容

  • 没有找到相关文章

最新更新