我在Google Maps中遇到问题,当我单击群集项时,他总是返回null。我收到了对应的数据,但是我想单击标记并更改背景(处于选定状态),因此我可以从标记中检索信息,但不能与自定义渲染器通信。
我正在使用这样的自定义渲染器:
inner class StationRenderer(context: Context, map: GoogleMap,
clusterManager: ClusterManager<Station>) : DefaultClusterRenderer<Station>(context, map, clusterManager) {
override fun onBeforeClusterRendered(cluster: Cluster<Station>?, markerOptions: MarkerOptions?) {
markerOptions?.icon(BitmapDescriptorFactory.fromBitmap(createStoreMarker(cluster?.size.toString())))
}
override fun onClusterItemRendered(clusterItem: Station?, marker: Marker?) {
super.onClusterItemRendered(clusterItem, marker)
clusterMarkerMap[clusterItem!!] = marker!!
}
override fun onBeforeClusterItemRendered(item: Station?, markerOptions: MarkerOptions?) {
markerOptions?.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_map_pin))
}
private fun createStoreMarker(stationsCount:String): Bitmap {
val markerLayout = layoutInflater.inflate(R.layout.marker_item, null)
val markerRating = markerLayout.findViewById(R.id.marker_text) as TextView
markerRating.text = stationsCount
markerLayout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
markerLayout.layout(0, 0, markerLayout.getMeasuredWidth(), markerLayout.getMeasuredHeight())
val bitmap = Bitmap.createBitmap(markerLayout.getMeasuredWidth(), markerLayout.getMeasuredHeight(), Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
markerLayout.draw(canvas)
return bitmap
}
}
这是我的onClusterItemClick:
override fun onClusterItemClick(p0: Station?): Boolean {
dragView.visibility = View.VISIBLE
Toast.makeText(context,p0?.name,Toast.LENGTH_LONG).show()
val marker = StationRenderer(context!!, mGoogleMaps, mClusterManager!!).getMarker(p0) //this is always null
marker?.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ic_map_pin_selected))
return true
}
这是我的onCreate
override fun doOnCreated() {
mapView.onCreate(null)
mapView.onResume()
mapView.getMapAsync(this)
viewModel.getStations(activity?.supportFragmentManager!!, R.id.fragment_container, context!!)
viewModel.retrieveStations().observe(this, Observer<List<Station>> {
addItems(it)
mClusterManager?.addItems(stationList)
})
}
这是我的onMapReady
override fun onMapReady(googleMap: GoogleMap) {
MapsInitializer.initialize(context )
mGoogleMaps = googleMap
googleMap.mapType = GoogleMap.MAP_TYPE_NORMAL
//mGoogleMaps.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(51.503186, -0.126446), 10f))
mClusterManager = ClusterManager(context, mGoogleMaps)
mClusterManager?.setRenderer(StationRenderer(context!!,mGoogleMaps, mClusterManager!!))
mGoogleMaps.setOnCameraIdleListener(mClusterManager)
mGoogleMaps.setOnMarkerClickListener(mClusterManager)
mClusterManager?.setOnClusterClickListener(this)
mClusterManager?.setOnClusterItemClickListener(this)
}