如何使用Kotlin和Google Maps utils在Google Map中对标记进行聚类

时间:2018-11-17 12:19:58

标签: android google-maps kotlin

我目前正面临这个问题[您可以在下面的图片中看到

Too many markers picture

如何对所有标记进行聚类,以便在开始时仅显示6个BIG标记中的5个,以指示标记在哪里,如果缩放,他会增加标记的数量而标记的大小会减小。

这是我的代码

这是我的MapFragment

class MapFragment: BaseFragment<FragmentMapBinding, MapViewModel>(),OnMapReadyCallback , GoogleMap.OnCameraMoveListener{


    private var stationList = mutableListOf<Station>()

    override fun onCameraMove() {

    }

    lateinit var mGoogleMaps:GoogleMap

    override fun onMapReady(googleMap: GoogleMap) {
        MapsInitializer.initialize(context )
        mGoogleMaps = googleMap
        googleMap.mapType = GoogleMap.MAP_TYPE_NORMAL
    }



    companion object {
        fun newInstance() = MapFragment()
    }

    @Inject
    lateinit var viewModelFactory: ViewModelProvider.Factory


    override fun layoutToInflate() = R.layout.fragment_map

    override fun defineViewModel() = ViewModelProviders.of(this, viewModelFactory).get(MapViewModel::class.java)

    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)
            for (i in 0 until stationList.size) {
                val name = LatLng(stationList[i].latitude, stationList[i].longitude)
                mGoogleMaps.addMarker(MarkerOptions()
                        .position(name)
                        //.title(name)
                        //.snippet(name.toString() + " is cool")
                        // .icon(BitmapDescriptorFactory
                        //       .fromResource(R.drawable.ic_map_pin )))
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)))

            }
        })
    }

    fun addItems(items: List<Station>){
        stationList.addAll(items)
    }

与此相对应的其他类是API请求,因此出于简短代码的目的,我从API的每个站点接收了所有long和lat。我已经看到了集群google maps utils,但是我不能将其发挥作用。有人可以帮忙吗?

这是我的模特

data class Station(
        val id: Int,
        val name: String,
        val latitude: Double,
        val longitude: Double,
        val address: String,
        val post_code: String,
        val location: String,
        val serviceStore: Int,
        val serviceMechanicalWorkshop: Int,
        val serviceRestaurant: Int,
        val serviceWash: Int
)

0 个答案:

没有答案