如何在Google地图中选择和取消选择标记?

时间:2018-11-18 15:47:02

标签: android google-maps google-maps-markers

我有一个使用标记在Google地图中标记的地点的列表。我想选择一个标记,以使其以不同的颜色突出显示。当我单击同一标记或任何其他标记时,我要删除在第一个标记中所做的选择,并将其设置回默认颜色。

这是我的onClusterItemClick方法

override fun onClusterItemClick(p0: Station?): Boolean {
        dragView.visibility = View.VISIBLE
        viewModel.loadStation(p0?.id!!)
        val marker = renderer.getMarker(p0)

        //save previous merker here

 marker?.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ic_map_pin_selected))

        return true
    }

这是我的Station Renderer

/**
     * Class to design the pin point into the map
     */

    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 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 markerImage = markerLayout.findViewById(R.id.marker_image) as ImageView
            val markerRating = markerLayout.findViewById(R.id.marker_text) as TextView
            markerImage.setImageResource(R.drawable.ic_map_pin)
            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
        }


        override fun shouldRenderAsCluster(cluster: Cluster<Station>?): Boolean {
            return cluster?.size !!> 1
        }


    }

1 个答案:

答案 0 :(得分:0)

在googleMaps中,这些不是被选中或取消选中的东西,也不是专门用于but的{​​{1}}监听器,您可以使用此监听器并添加一些逻辑来实现该目标。

onMarkerClick();

您可以从中获得“创意”:How to select and deselect a marker in google maps in android?