【发布时间】:2020-06-14 07:00:28
【问题描述】:
vienna 取当前位置有困难,谁能指导我,vienna 的值是静态的只能定义,我想让你取当前位置。有人可以帮助我吗,谢谢。欢迎任何帮助
实用程序代码
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()
}
}
代码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)
}
【问题讨论】:
-
您如何请求位置?
-
您好,感谢您的回复。通过单击地图手动创建标记时很好,他可以获得当前位置,但是当他单击快速拨号按钮时,他会得到 utils.vienna,我需要修改 utils.vienna 以使用 currentlocation 问题在所有方面我试过犯错误发生。
-
因为你有
currentLocation变量,当你点击地图上的新位置时,你需要修改它的值。 [所以当用户点击不是来自 GPS 的某个位置时,您会在地图中获得位置?] -
val position = if (currentLocation != null) LatLng(currentLocation!!.latitude, currentLocation!!.longitude) else Utils.currentLocation 给出这个错误 kotlin.UninitializedPropertyAccessException: lateinit property currentLocation has not been initialized
-
我正在更新帖子以获取更多信息
标签: java android android-studio google-maps kotlin