Skip to content Skip to sidebar Skip to footer

Unable To Initialize Googlemap Object In Kotlin

Attempting to create a member variable mMap throws an error 'Classifier GoogleMap does not have a companion object, and thus must be initialized here'. In Java, I would have just u

Solution 1:

initialize GoogleMap like this

privatelateinitvar mMap: GoogleMap? = null

Implement the OnMapReadyCallback interface and use the onMapReady(GoogleMap) callback method to get a handle to the GoogleMap object. The GoogleMap object is the internal representation of the map itself. To set the view options for a map, you modify its GoogleMap object.

classMainActivity : OnMapReadyCallback {// your other codeoverridefunonMapReady(googleMap: GoogleMap) {
mMap = googleMap  

if (mMap != null) {
    val permission = ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)

    if (permission == PackageManager.PERMISSION_GRANTED) {
        mMap?.isMyLocationEnabled = true
    } else {
        requestPermission(
                Manifest.permission.ACCESS_FINE_LOCATION,
                LOCATION_REQUEST_CODE)
     }
 }
 }
}

Solution 2:

I tried to apply it

private lateinit var map: GoogleMap? = null

but I'm getting

'lateinit' modifier is not allowed on properties of nullable types

Post a Comment for "Unable To Initialize Googlemap Object In Kotlin"