В моем приложении я хочу использовать 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,
как я могу это исправить?