Java Android获取位置、保存并加载用户错误
我想在Android中获取位置,然后将其保存到服务器,然后显示用户列表 我的代码正在工作,但有时用户没有加载,我认为这是因为我管理位置请求的方式 这是我第一次与location合作,我将非常感谢任何关于我做错了什么的帮助或建议 非常感谢!我的代码:Java Android获取位置、保存并加载用户错误,java,android,kotlin,geolocation,Java,Android,Kotlin,Geolocation,我想在Android中获取位置,然后将其保存到服务器,然后显示用户列表 我的代码正在工作,但有时用户没有加载,我认为这是因为我管理位置请求的方式 这是我第一次与location合作,我将非常感谢任何关于我做错了什么的帮助或建议 非常感谢!我的代码: // Geolocation private lateinit var fusedLocationClient: FusedLocationProviderClient private lateinit var userLocationCa
// Geolocation
private lateinit var fusedLocationClient: FusedLocationProviderClient
private lateinit var userLocationCallback: LocationCallback
override fun onCreate(savedInstanceState: Bundle?) {
// Geolocation
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
// I call the function that geolocates and loads the users
getLastKnownLocation()
}
fun loadGrid() {
// This function loads users into a recyclerview
}
fun getLastKnownLocation() {
// Check permissions, if geolocation is not activated we don't call loadGrid function
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
val dialog = AlertDialog.Builder(this)
dialog.setMessage(R.string.txt_noloc)
dialog.setPositiveButton(R.string.txt_sett) { _, _ ->
val myIntent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
startActivity(myIntent)
}
dialog.setNegativeButton(R.string.text_cancel) { _, _ ->
finish()
}
dialog.setCancelable(false)
dialog.show()
}
// We make a location request
val userLocationRequest = LocationRequest().apply {
interval = 1000
fastestInterval = 1000
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}
userLocationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) {
locationResult ?: return
}
}
fusedLocationClient.requestLocationUpdates(userLocationRequest, userLocationCallback, null)
fusedLocationClient.lastLocation
.addOnSuccessListener { location ->
if (location != null) {
// use your location object
// get latitude , longitude and other info from this
val addresses: List<Address>?
val geoCoder = Geocoder(this, Locale.getDefault())
addresses = geoCoder.getFromLocation(
location.latitude,
location.longitude,
1
)
val state: String = addresses[0].subAdminArea
val country: String = addresses[0].countryName
// This saves data in the server
SaveLocation(
this,
user_token,
location.latitude.toString(),
location.longitude.toString(),
country,
state
).refreshLocation()
SaveLocation(
this,
user_token,
location.latitude.toString(),
location.longitude.toString(),
country,
state
).refreshLocationName()
// We load the users into the recyclerview
loadGrid()
}
}
}
//地理定位
私有lateinit变量fusedLocationClient:FusedLocationProviderClient
私有lateinit var userLocationCallback:LocationCallback
重写创建时的乐趣(savedInstanceState:Bundle?){
//地理定位
fusedLocationClient=LocationServices.getFusedLocationProviderClient(此)
//我调用对用户进行地理定位和加载的函数
getLastKnownLocation()
}
fun loadGrid(){
//此函数用于将用户加载到recyclerview中
}
有趣的getLastKnownLocation(){
//检查权限,如果地理位置未激活,则不调用loadGrid函数
如果(ActivityCompat.checkSelfPermission(
这
Manifest.permission.ACCESS\u FINE\u位置
)!=PackageManager.PERMISSION\u已授予和&ActivityCompat.checkSelfPermission(
这
Manifest.permission.ACCESS\u位置
)!=已授予PackageManager.PERMISSION\u权限
) {
val dialog=AlertDialog.Builder(此)
dialog.setMessage(R.string.txt\u noloc)
dialog.setPositiveButton(R.string.txt_sett){{},{->
val myIntent=Intent(设置、操作、位置、源、设置)
星触觉(myIntent)
}
dialog.setNegativeButton(R.string.text_cancel){{},{->
完成()
}
对话框。可设置可取消(false)
dialog.show()
}
//我们提出了一个定位请求
val userLocationRequest=LocationRequest()。应用{
间隔=1000
快速区间=1000
优先级=位置请求。优先级\u高\u精度
}
userLocationCallback=对象:LocationCallback(){
覆盖趣味onLocationResult(locationResult:locationResult?){
locationResult?:返回
}
}
fusedLocationClient.RequestLocationUpdate(userLocationRequest,userLocationCallback,null)
fusedLocationClient.lastLocation
.addOnSuccessListener{location->
如果(位置!=null){
//使用您的位置对象
//从中获取纬度、经度和其他信息
val地址:列表?
val geoCoder=geoCoder(这是Locale.getDefault())
地址=geoCoder.getFromLocation(
位置,纬度,
地点,经度,
1.
)
val状态:字符串=地址[0]。子管理员区域
val国家:字符串=地址[0]。国家名称
//这将在服务器中保存数据
保存位置(
这
用户令牌,
location.latitude.toString(),
location.longitude.toString(),
国家,,
状态
).refreshLocation()
保存位置(
这
用户令牌,
location.latitude.toString(),
location.longitude.toString(),
国家,,
状态
).refreshLocationName()
//我们将用户加载到recyclerview中
loadGrid()
}
}
}