我有一个包含片段作为地图的屏幕
XML
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/design_default_color_background"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ToolbarColoredBackArrow">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView_back"
android:layout_width="48dp"
android:layout_height="48dp"
android:padding="12dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/back" />
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:textColor="@color/colorTextBlack"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.Toolbar>
<fragment
app:layout_constraintTop_toBottomOf="@id/toolbar"
app:layout_constraintBottom_toTopOf="@id/ll_bottom"
android:id="@+id/googleMap"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
/>
<LinearLayout
android:id="@+id/ll_bottom"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
android:gravity="center">
<View
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="100dp"
android:layout_height="8dp"
android:background="@drawable/bottom_sheet_divider"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
在OnViewCreated中,我首先设置映射,然后设置viewModel然后我得到捆绑包并在viewModel中设置值,以观察变量
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setupMap()
viewModel = ViewModelProvider(requireActivity(), viewModelFactory).get(MapNewTwoViewModel::class.java)
setupViewModel()
getBundle()
}
SetupMap 没有什么特别之处
private fun setupMap(){
val fragment = childFragmentManager.findFragmentById(R.id.googleMap) as SupportMapFragment
fragment.getMapAsync(this)
}
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 (isClient) {
viewModel.setType(MapNewTwoViewModel.OrderType.MY)
} else
viewModel.setType(MapNewTwoViewModel.OrderType.ALL)
}
}
setupCluster
private fun setupCluster(map: GoogleMap) {
clusterManager = ClusterManager(MyApplication.applicationContext(), map)
renderer = DotRenderer()
clusterManager?.renderer = renderer
if(clusterManager != null){
Log.d("ClusterManager", "true")
} else {
Log.d("ClusterManager", "false")
}
map.setOnCameraIdleListener(clusterManager)
map.setOnMarkerClickListener(clusterManager)
map.setOnInfoWindowClickListener(clusterManager)
clusterManager?.setOnClusterClickListener(this)
clusterManager?.setOnClusterItemClickListener(this)
}
在setupViewModel中,我只是在观看MediatorLiveData
viewModel.mediatorLiveData.observe(viewLifecycleOwner, {
Log.d("MediatorLiveData", it.size.toString())
setItemsCluster(it)
})
集群所需的setItemsCluster
private fun setItemsCluster(orders: List<OrderX>){
//---somesthing Operation/.
if(clusterManager == null){
Log.d("ClusterManager", "null")
}
clusterManager?.addItems(itemClusters)
clusterManager?.cluster()
}
现在日志:
2020-10-21 18:53:23.658 30104-30104/com.app D/ClusterManager: null
2020-10-21 18:53:23.722 30104-30104/com.app D/ClusterManager: true
2020-10-21 18:53:23.734 30104-30104/com.app D/OnClustersChanged: []
问题,为什么要使用viewModel.oobserve((工作速度比onMapReady快?
毕竟,我调用它的时间晚于setupMap(onMapReady,其中初始化了Cluster(
如何修复?因此,我不向ClusterManager 添加点数
在地图准备好后为其设置绑定。像这样:
override fun onMapReady(map: GoogleMap?) {
...
setupMapBindings()
}
fun setupMapBindings() {
viewModel.mediatorLiveData.observe(viewLifecycleOwner, {
setItemsCluster(it)
})
}
getMapAsync是异步的(显然是它的名称(,并且需要一些时间才能准备好映射,当您最初观察LiveData时,它将返回其当前值,因此您总是有一个值