首先,代碼:
class MapFragment: BaseFragment<MapFragmentBinding>(R.layout.map_fragment) {
var mapView: MapView? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
mapView = binding.mapView
MapsInitializer.initialize(requireContext(),
MapsInitializer.Renderer.LATEST
) {}
// seems a bit out of place, but due to the binding variable, from out baseFragment class, it has to be done here
mapView?.onCreate(savedInstanceState)
mapView?.getMapAsync { gMap ->
gMap.setOnMapLoadedCallback {
val randomLocation = LatLng(
Random.nextDouble(-170.0, 170.0),
Random.nextDouble(-170.0, 170.0)
)
gMap.addMarker(MarkerOptions().position(randomLocation).title(randomLocation.toString()))
gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(randomLocation, 10.0f))
}
}
}
override fun onStart() {
super.onStart()
mapView?.onStart()
}
override fun onPause() {
super.onPause()
mapView?.onPause()
}
override fun onResume() {
super.onResume()
mapView?.onResume()
}
override fun onStop() {
super.onStop()
mapView?.onStop()
}
override fun onLowMemory() {
super.onLowMemory()
mapView?.onLowMemory()
}
override fun onDestroy() {
super.onDestroy()
mapView?.onDestroy()
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
mapView?.onSaveInstanceState(outState)
}
}
這是一個包含在另一個片段中的 mapFragment。它經常被重新加載(準確地說,應用程式有一個充滿專案的回收器視圖,只要按下這個片段就可見)。
我不知道為什么,但有時(每當它打開和關閉 3-7 次時)它會將相機影片到隨機位置,并且根本不顯示標記。我注意到它主要發生在“隨機位置”在北冰洋或南極洲時(相信我,我根本不知道這是否相關,我和你一樣困惑)。
任何提示/想法為什么會發生這種情況?
uj5u.com熱心網友回復:
您的隨機緯度范圍無效-170.0, 170.0。地圖相機實作可能會將超出范圍的值固定為 -90 或 90,并且地圖將拒絕放置標記。
隨機位置的最大范圍可以使用:
val randomLocation = LatLng(
Random.nextDouble(-90.0, 90.0), // kotlin Random produces [-90,90)
Random.nextDouble(-180.0, 180.0)
緯度的地圖 API 允許:
[-90, 90] // inclusive at both ends
對于經度許可:
[-180, 180) // inclusive on the negative and exclusive on the positive
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/514414.html
下一篇:清潔架構中的服務
