Skip to content Skip to sidebar Skip to footer

Kotlin With Map In Android

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View { var view: View = inflater?.inflate(R.layout.map_fragment, null)!!

Solution 1:

You declared mapFragment to be nullable so you have to deal with it:

var mapFragment : SupportMapFragment?=null
mapFragment = fragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
mapFragment?.getMapAsync(this)

Solution 2:

The error implies that you try to cast a null to SupportMapFragment. So, it will be safer to check the type first.

val mapFragment = fragmentManager.findFragmentById(R.id.map)
if (mapFragment is SupportMapFragment) {
    mapFragment.getMapAsync(this)
}

Solution 3:

privatelateinitvar mMap: GoogleMap

 overridefunonCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val mapFragment: SupportMapFragment? = map as SupportMapFragment?
    mapFragment?.getMapAsync(this)
}




overridefunonMapReady(googleMap: GoogleMap?) {

    if (googleMap != null) {
        mMap = googleMap
    }

    val sydney = LatLng(-34.0, 151.0)
    mMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney"))
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
}

Post a Comment for "Kotlin With Map In Android"