KotlinNullPointerException при попытке получить мое текущее местоположение с помощью mapbox в kotlin - PullRequest
0 голосов
/ 26 октября 2019

Пытаясь получить мое текущее местоположение и сохранить данные Latlng в переменную, чтобы я мог использовать их позже, я просто использую mapbox SDK с enableLocationComponent, вот код, который я использую:


    private var myCurrentLocation: LatLng? = null



    @SuppressLint("MissingPermission")
    private fun enableLocationComponent(loadedMapStyle: Style) {
// Check if permissions are enabled and if not request
        if (PermissionsManager.areLocationPermissionsGranted(this)) {

// Create and customize the LocationComponent's options
            val customLocationComponentOptions = LocationComponentOptions.builder(this)
                .trackingGesturesManagement(true)
                .accuracyColor(ContextCompat.getColor(this, R.color.colorGreen))
                .build()

            val locationComponentActivationOptions =
                LocationComponentActivationOptions.builder(this, loadedMapStyle)
                    .locationComponentOptions(customLocationComponentOptions)
                    .build()


// Get an instance of the LocationComponent and then adjust its settings
            mapboxMap.locationComponent.apply {

                // Activate the LocationComponent with options
                activateLocationComponent(locationComponentActivationOptions)

// Enable to make the LocationComponent visible
                isLocationComponentEnabled = true

// Set the LocationComponent's camera mode
                cameraMode = CameraMode.TRACKING

// Set the LocationComponent's render mode
                renderMode = RenderMode.COMPASS

                myCurrentLocation = LatLng(
                    mapboxMap.locationComponent.lastKnownLocation?.latitude!!,
                    mapboxMap.locationComponent.lastKnownLocation!!.longitude)
            }
        } else {
            permissionsManager = PermissionsManager(this)
            permissionsManager.requestLocationPermissions(this)
        }

    }

Позже, когда я пытаюсь запустить этот блок, я получаю ошибку:

            val myPosition = CameraPosition.Builder()
                .target(myCurrentLocation)
                .zoom(14.0)
                .tilt(20.0)
                .build()
            mapboxMap.animateCamera(
                CameraUpdateFactory
                    .newCameraPosition(myPosition), 7000
            )

ошибка kotlin.KotlinNullPointerException, указывающая на строку myCurrentLocation = LatLng( mapboxMap.locationComponent.lastKnownLocation?.latitude!!, mapboxMap.locationComponent.lastKnownLocation!!.longitude)

...