MapBox和Kotlin的入门者。在启用位置权限后首次安装APK时,我的LocationComponent不会激活,也不会显示在地图上。该应用程序将崩溃,并且位置组件将正常显示,并且该应用程序将按预期运行。
在启用位置权限后,任何人都可以提供一些帮助来使我的LocationComponent激活并显示吗?
干杯!
2020-01-17 14:38:16.768 16702-16702/com.example.carparker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.carparker, PID: 16702
com.mapbox.mapboxsdk.location.LocationComponentNotInitializedException: The LocationComponent has to be activated with one of the LocationComponent#activateLocationComponent overloads before any other methods are invoked.
at com.mapbox.mapboxsdk.location.LocationComponent.checkActivationState(LocationComponent.java:1606)
at com.mapbox.mapboxsdk.location.LocationComponent.getLastKnownLocation(LocationComponent.java:1030)
at com.example.carparker.MainActivity.centreCamera(MainActivity.kt:176)
at com.example.carparker.MainActivity$onCreate$4.onClick(MainActivity.kt:125)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
fun onCreate() {
mapView = findViewById(R.id.mapView)
parkButton = findViewById(R.id.parkBtn)
mapView.onCreate(savedInstanceState)
mapView?.getMapAsync {mapboxMap ->
map = mapboxMap
map.setStyle(Style.MAPBOX_STREETS) {
Log.d(TAG, "Enable location component")
enableLocation()
enableLocationComponent(it)
}
/**
* Enables permissions for location
*/
fun enableLocation() {
if (PermissionsManager.areLocationPermissionsGranted(this)) {
} else {
permissionManager = PermissionsManager(this)
permissionManager.requestLocationPermissions(this)
}
}
/**
* Actions enable location again
*/
override fun onPermissionResult(granted: Boolean) {
if (granted) {
enableLocation()
}
}
/**
* Enables user tracking and displaying the location puck.
*/
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.mapbox_blue))
.build()
val locationComponentActivationOptions = LocationComponentActivationOptions.builder(this, loadedMapStyle)
.locationComponentOptions(customLocationComponentOptions)
.build()
// Get an instance of the LocationComponent and then adjust its settings
map.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
}
} else {
permissionManager = PermissionsManager(this)
permissionManager.requestLocationPermissions(this)
}
}
答案 0 :(得分:1)
您要复制很多权限检查和初始化。
有关如何进行基本LocationComponent
设置的准系统Kotlin示例,请参见https://github.com/mapbox/mapbox-android-demo/blob/master/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/location/KotlinLocationComponentActivity.kt。