谷歌地圖沒有在片段中初始化。即使在嘗試呼叫getMapAsync不同的片段生命周期之后
class MyMapFragment : Fragment(), OnMapReadyCallback {
private lateinit var map: GoogleMap
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
//...binding
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val mapFragment = childFragmentManager.findFragmentById(R.id.r_map) as SupportMapFragment?
mapFragment?.getMapAsync(this)
//...
}
override fun onMapReady(googleMap: GoogleMap) {
map = googleMap
}
private fun configureMap(
start: Pair<Double, Double>,
end: Pair<Double, Double>,
) {
//Here the map says it's not initialized
//so the condition fails
if (this::map.isInitialized) {
drawPolyLine(start.first, start.second, end.first, end.second)
//...
}
}
//...
}
uj5u.com熱心網友回復:
configureMap我打電話onMapReady感謝Ammar Abdullah
override fun onMapReady(googleMap: GoogleMap) {
map = googleMap
configureMap(//...)
}
private fun configureMap(
start: Pair<Double, Double>,
end: Pair<Double, Double>,
) {
//Here the map says it's not initialized
//so the condition fails
if (this::map.isInitialized) {
drawPolyLine(start.first, start.second, end.first, end.second)
//...
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/490097.html
標籤:安卓 科特林 谷歌地图 安卓片段 谷歌地图-android-api-3
