gpt4 book ai didi

java - 当前位置的android google maps问题

转载 作者:行者123 更新时间:2023-12-02 12:57:12 27 4
gpt4 key购买 nike

我在选择当前位置时遇到了困难,有人可以指导我,只能定义静态值,我希望您选择当前位置。有人可以帮助我,谢谢。欢迎任何帮助

实用程序代码

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()
}
}

代码片段
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)


}

最佳答案

问题在这里

kotlin.UninitializedPropertyAccessException: lateinit property currentLocation has not been initialized

发生这种情况是因为您的 lateinit var currentLocation: LatLng从未在 init{}块中初始化。创建该类的新实例时,将调用该类的 init{}块。但是,您的 currentLocationcompanion object中的变量,因此当您以这种方式调用它时,您的类就永远不会创建对象,并且 currentLocation也不会初始化。

您可以通过使用object而不是class来解决此问题。
object Utils  {
lateinit var currentLocation: LatLng
init {
currentLocation = `...`
}

}

关于java - 当前位置的android google maps问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60481102/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com