在我的应用程序中,我想使用 mapBox 在我的应用程序中使用地图,并且希望在加载应用程序时显示当前位置。
我写下面的代码,但是当运行应用程序时,在 logcat (不强制关闭)和不加载地图中向我显示错误!
我的活动代码:
class AddressMapPage : BaseActivity(), LocationEngineListener {
override var layoutId: Int = R.layout.activity_address_map_page
override var context: Context = this
private lateinit var mapirMap: MapirMap
private lateinit var latLng: LatLng
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//Hide status bar
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
)
(supportFragmentManager.findFragmentById(R.id.myMapView) as SupportMapFragment)
.getMapAsync { mapirMap ->
this@AddressMapPage.mapirMap = mapirMap
imageView2.setOnClickListener {
latLng = mapirMap.cameraPosition.target
toast(latLng.toString())
Log.e("TalAndLog", latLng.toString())
}
getLocationPermission()
}
}
private fun getLocationPermission() {
KotlinPermissions.with(this)
.permissions(Manifest.permission.ACCESS_FINE_LOCATION)
.onAccepted {
currentLocation()
}
.onDenied {
toast("Not allowed error message")
}
.onForeverDenied {
toast("Not allowed error message")
}
.ask()
}
private fun currentLocation() {
val component = mapirMap.locationComponent
component.activateLocationComponent(context)
component.isLocationComponentEnabled = true
if (component.locationEngine != null) {
component.locationEngine!!.addLocationEngineListener(this)
}
mapirMap.animateCamera(
CameraUpdateFactory
.newLatLngZoom(
LatLng(
component.lastKnownLocation!!.latitude,
component.lastKnownLocation!!.longitude
), 14.0
)
)
}
override fun onLocationChanged(location: Location?) {
if (location != null)
mapirMap.animateCamera(
CameraUpdateFactory.newLatLngZoom(
LatLng(
location.latitude,
location.longitude
), 14.0
)
)
}
override fun onConnected() {
}
}
Logcat错误:
kotlin.KotlinNullPointerException
at com.app.ui.address.map.AddressMapPage.currentLocation(AddressMapPage.kt:77)
at com.app.ui.address.map.AddressMapPage.access$currentLocation(AddressMapPage.kt:20)
at com.app.ui.address.map.AddressMapPage$getLocationPermission$1.invoke(AddressMapPage.kt:55)
at com.app.ui.address.map.AddressMapPage$getLocationPermission$1.invoke(AddressMapPage.kt:20)
at com.kotlinpermissions.KotlinPermissions$PermissionCore$onAccepted$1.onResult(KotlinPermissions.kt:56)
at com.kotlinpermissions.KotlinPermissions$PermissionCore.onReceivedPermissionResult$kotlin_permissions_release(KotlinPermissions.kt:36)
at com.kotlinpermissions.KotlinPermissions$PermissionCore.onAcceptedPermission(KotlinPermissions.kt:132)
at com.kotlinpermissions.KotlinPermissions$PermissionCore.ask(KotlinPermissions.kt:107)
at com.app.ui.address.map.AddressMapPage.getLocationPermission(AddressMapPage.kt:63)
at com.app.ui.address.map.AddressMapPage.access$getLocationPermission(AddressMapPage.kt:20)
at com.app.ui.address.map.AddressMapPage$onCreate$1.onMapReady(AddressMapPage.kt:47)
at ir.map.sdk_map.maps.SupportMapFragment.onMapReady(SupportMapFragment.java:124)
at ir.map.sdk_map.maps.MapView$MapCallback.onMapReady(MapView.java:1663)
at ir.map.sdk_map.maps.MapView$MapCallback.onDidFinishLoadingStyle(MapView.java:1701)
at ir.map.sdk_map.maps.MapChangeReceiver.onDidFinishLoadingStyle(MapChangeReceiver.java:195)
at ir.map.sdk_map.maps.NativeMapView.onDidFinishLoadingStyle(NativeMapView.java:995)
at android.os.MessageQueue.nativePollOnce(Native Method)
为此行显示错误:component.lastKnownLocation!!.latitude,
我该如何解决?
答案 0 :(得分:0)
This example显示了推荐的用于在地图上显示用户位置的方法。
由于您使用的是Kotlin的not-null assertion operator(!!
),因此由于kotlin.KotlinNullPointerException
为空而抛出了component.lastKnownLocation
。在链接的示例中不会发生此问题,因为在enableLocationComponent
回调中调用了onMapReady
方法。 enableLocationComponent
方法执行必要的设置,以将LocationComponent
实例与Mapbox地图一起使用。