嗨,我正在使用 FusedLocationClient 从用户那里获取位置。它的工作我能够定期获取纬度和经度,但有时显示错误的距离会在几秒钟内从 10.0 变为 200.0,然后返回到 10.0。我想向用户展示他们距离商店 200m。但即使他们距离商店只有 10m,应用程序有时也会显示他们距离商店 200m。
我能做些什么来提高定位客户端的准确性吗?
class LocServices : Service() {
private val TAG = ""
private val UPDATE_INTERVAL_IN_MILLISECONDS: Long = 5000
private val FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2
private var mLocationRequest: LocationRequest? = null
private var mFusedLocationClient: FusedLocationProviderClient? = null
private var mLocationCallback: LocationCallback? = null
private var mLocation: Location? = null
var existLongitude: String? = null
var existLatitude: String? = null
var sharedPrefs: SharedPreferences? = null
override fun onCreate() {
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
mLocationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) {
super.onLocationResult(locationResult)
onNewLocation(locationResult!!.lastLocation)
}
}
createLocationRequest()
getLastLocation()
requestLocationUpdates()
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.i(TAG, "Service started")
return Service.START_STICKY
}
override fun onBind(intent: Intent): IBinder? {
return null
}
private fun requestLocationUpdates() {
sharedPrefs = this.getSharedPreferences("GEO", Context.MODE_PRIVATE)
if (sharedPrefs != null) {
existLatitude = sharedPrefs!!.getString("LAT", null)
existLongitude = sharedPrefs!!.getString("LONG", null)
}
if (existLatitude != null && existLongitude != null) {
Log.i(TAG, "Requesting location updates")
setRequestingLocationUpdates(this, true)
try {
mFusedLocationClient!!.requestLocationUpdates(mLocationRequest, mLocationCallback!!, Looper.myLooper())
} catch (unlikely: SecurityException) {
setRequestingLocationUpdates(this, false)
Log.e(TAG, "Lost location permission. Could not request updates. $unlikely")
}
}
}
private fun getLastLocation() {
try {
mFusedLocationClient!!.lastLocation
.addOnCompleteListener { task ->
if (task.isSuccessful && task.result != null) {
mLocation = task.result
} else {
Log.w(TAG, "Failed to get location.")
}
}
} catch (unlikely: SecurityException) {
Log.e(TAG, "Lost location permission.$unlikely")
}
}
private fun onNewLocation(location: Location) {
Log.i(TAG, "New location: $location")
mLocation = location
if (existLatitude != null && existLongitude != null) {
val selected_location = Location("locationA")
selected_location.latitude = existLatitude!!.toDouble()
selected_location.longitude = existLongitude!!.toDouble()
val near_locations = Location("locationB")
near_locations.latitude = mLocation!!.latitude
near_locations.longitude = mLocation!!.longitude
val distance = selected_location.distanceTo(near_locations)
Toast.makeText(this, distance.toString(), Toast.LENGTH_SHORT).show()
if (distance > 53.0) {
Toast.makeText(this, "You are outside $distance", Toast.LENGTH_SHORT).show()
}
}
}
private fun createLocationRequest() {
mLocationRequest = LocationRequest()
mLocationRequest!!.interval = UPDATE_INTERVAL_IN_MILLISECONDS
mLocationRequest!!.fastestInterval = FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS
mLocationRequest!!.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}
companion object {
val KEY_REQUESTING_LOCATION_UPDATES = "requesting_locaction_updates"
fun setRequestingLocationUpdates(context: Context, requestingLocationUpdates: Boolean) {
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putBoolean(KEY_REQUESTING_LOCATION_UPDATES, requestingLocationUpdates)
.apply()
}
}
}