android - 用户位置不打印纬度/经度
问题描述
首先,我想检查用户的位置设置:
fun checkSettings() {
val builder = LocationSettingsRequest.Builder()
val client: SettingsClient = LocationServices.getSettingsClient(this)
val task: Task<LocationSettingsResponse> = client.checkLocationSettings(builder.build())
task.addOnSuccessListener { locationSettingsResponse ->
fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null)
}
task.addOnFailureListener { exception ->
if (exception is ResolvableApiException){
fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null)
try {
exception.startResolutionForResult(this@MainActivity, 500)
} catch (sendEx: IntentSender.SendIntentException) {
println("Error")
}
}
}
我在方法中添加了这个功能onCreate
。如果一切正常,这个其他函数onCreate
应该打印纬度和经度:
locationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) {
locationResult ?: return
for (location in locationResult.locations){
println(location.latitude)
println(location.longitude)
}
}
}
但它不起作用。我很抱歉,但我很困惑,我不知道我应该做什么。有任何想法吗?
解决方案
推荐阅读
- sql - 如何在 SSMS 中合并具有不同数量和列顺序的两个表?
- mongodb - 如何使用 Mongo CLI 保存较大的文件
- java - Querydsl 绑定 - 如何设置绑定以便参数可以使用 Like 和 Contains
- javascript - 我如何在 ReactJS 上比较和连接两个数组
- ios - 如何根据点击的单元格更改数组
- ios - iOS N 如何获取所有用户代理字符串
- python - 在 Keras 中导入 Attention 包会出现 ModuleNotFoundError: No module named 'attention'
- xcode11 - 当项目包含资产目录时,Xcode 11-beta3 构建失败:错误:未知参数“--development-region”
- docker - NTLM Kerberos 支持在 nginx 服务器后面设置的身份服务器(不适用于 IE)
- python - 网络抓取 pubmeds 2., 3., 4... 页面