I'm having difficulties for vienna to take the current location, someone could guide me, the value of vienna is static can only be defined, I wanted you to take the current location. could someone help me, thanks. any help is welcome
Utils code
import map.me.models.Issue
import com.google.android.gms.maps.model.LatLng
class Utils (location: LatLng){
companion object {
lateinit var currentLocation: LatLng
var vienna= LatLng(-23.5629, -46.6544)
var markers = ArrayList<Issue>()
}
init {
vienna = LatLng(-23.5629, -46.6544)
currentLocation = location
markers = ArrayList()
}
}
code mapfrag
override fun onMapReady(googleMap: GoogleMap) {
Log.i("MAP READY", "READY")
val position = if (currentLocation != null) LatLng(currentLocation!!.latitude, currentLocation!!.longitude) else Utils.vienna
this.map = googleMap
this.map!!.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 15f)) // Vienna
getFineLocationPermission()
this.map!!.setOnMarkerClickListener(this)
this.map!!.uiSettings.isRotateGesturesEnabled = true
this.map!!.uiSettings.isZoomGesturesEnabled = true
this.map!!.setOnInfoWindowClickListener(this)
this.map!!.setOnMapLongClickListener(this)
}
How about your initialized your utils class firts? so you will not got error kotlin.UninitializedPropertyAccessException: lateinit property currentLocation
class Utils (){
companion object {
lateinit var currentLocation: LatLng
var vienna= LatLng(-23.5629, -46.6544)
var markers = ArrayList<Issue>()
}
fun initMyCurrentLocation(loc:LatLang?){
currentLocation = loc?: vienna
}
}
in your frag you can used this method like this:
override fun onMapReady(googleMap: GoogleMap) {
//init you currentLocationHere
Utils.initMyCurrentLocation('yourCurrentLocation')
//the rest code.
val position = if (currentLocation != null) LatLng(currentLocation!!.latitude, currentLocation!!.longitude) else Utils.vienna
this.map = googleMap
this.map!!.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 15f)) // Vienna
getFineLocationPermission()
this.map!!.setOnMarkerClickListener(this)
this.map!!.uiSettings.isRotateGesturesEnabled = true
this.map!!.uiSettings.isZoomGesturesEnabled = true
this.map!!.setOnInfoWindowClickListener(this)
this.map!!.setOnMapLongClickListener(this)
}
The problem is here
kotlin.UninitializedPropertyAccessException: lateinit property currentLocation has not been initialized
That happen because your lateinit var currentLocation: LatLng
never initialize in init{}
block. init{}
block for class will call when you create new Instance of that class. But your currentLocation
is a variable in companion object
, so when you call it that way your class is never created an object and currentLocation
will never initialized.
You can fix this by using object instead of class.
object Utils {
lateinit var currentLocation: LatLng
init {
currentLocation = `...`
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.