Google map is not being initialized in fragment. Even after trying to call getMapAsync
in different fragment lifecycles
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)
//...
}
}
//...
}
CodePudding user response:
I called the configureMap
on the onMapReady
thanks to 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)
//...
}
}