Home > Back-end >  Android Kotlin ARCore GeoSpatial API Type Mismatch Error: Earth? but Earth is Expected
Android Kotlin ARCore GeoSpatial API Type Mismatch Error: Earth? but Earth is Expected

Time:01-14

I'm trying to build a Geospatial AR app using Android ARCore and Maps for Android SDK.

However, when I try to run the app, it gives the error, "type mismatch: inferred type is Earth? but Earth was expected."

This error only occurs in the code line, "activity.view.updateStatusText(earth, earth.cameraGeospatialPose)" inside the HelloGeoRenderer.kt file.

However, when I comment out the code line above, the code works fine, the map appears below the app screen like is expected, but the AR anchor does not appear.

Can anyone help me spot the issue?

Thanks in advance!

    // Obtain Geospatial information and display it on the map.
    val earth = session.earth
    if (earth?.trackingState == TrackingState.TRACKING) {
      // The Earth object may be used here.
      val cameraGeospatialPose = earth.cameraGeospatialPose
      
      activity.view.mapView?.updateMapPosition(
        latitude = cameraGeospatialPose.latitude,
        longitude = cameraGeospatialPose.longitude,
        heading = cameraGeospatialPose.heading
      )
    }

    activity.view.updateStatusText(earth, earth.cameraGeospatialPose)


CodePudding user response:

Before getting var earth, it is necessary to configure the session to use GeospatialMode.ENABLED. Since it is possible to return null when Geospatial isn't enabled, the return type is a nullable Earth? instead of Earth, which may cause the type mismatch error.

To configure the session, you may write:

session.configure(
    config.apply {
        geospatialMode = Config.GeospatialMode.ENABLED
    }
)

Before setting such a configuration, it is important to check if the device is compatible with Geospatial API with the function:

if (session.isGeospatialModeSupported(Config.GeospatialMode.ENABLED)) {...}
  • Related