Home > Net >  onMapReady function seems to not be called on android app
onMapReady function seems to not be called on android app

Time:11-04

The map seems to be created but the marker does not get placed. Most questions relating to this problem seems to be missing getmapasync but th,s one seems to have it

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // Retrieve the content view that renders the map.
    setContentView(R.layout.fragment_dashboard)

    val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as? SupportMapFragment
    mapFragment?.getMapAsync(this)
}
override fun onMapReady(googleMap: GoogleMap) {
    mMap = googleMap
    val sydney = LatLng(-33.852, 151.211)
    mMap.addMarker(
        MarkerOptions()
            .position(sydney)
            .title("Marker in Sydney")
    )
    // [START_EXCLUDE silent]
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
    // [END_EXCLUDE]
}
// [END maps_marker_on_map_ready_add_marker]

CodePudding user response:

Is your map fragment nested in a fragment? If you are calling getMapAsync from a fragment that contains the actual map, you need to use childFragmentManager instead of supportFragmentManager to find the map

(childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment).getMapAsync(this)

  • Related