定期跟踪用户当前位置的更快方法是什么?
What is the Faster way to track users current location periodically?
我已经完成了我正在开发的产品,但是目前,我们跟踪用户(乘客)位置以及司机的速度太慢了。
这是我用来在地图移动时使用乘客/司机图标跟踪和更新地图的代码:
import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.location.Location
import android.os.Looper
import android.util.Log
import androidx.core.content.ContextCompat
import com.google.android.gms.location.*
import mobi.audax.tupi.motorista.bin.task.GeoDecodeTask
import mobi.audax.tupi.passageiro.util.Prefs
class IntermitentLocationThread(val context: Context, val onLocationUpdate: (location: Location?) -> Unit) : LocationListener {
private var UPDATE_INTERVAL = (1000 * 10).toLong() // 10 segundos de intervalo
private val MAX_WAIT_TIME = UPDATE_INTERVAL * 2 // 20 segundos
private var bestLocation: Location? = null
fun requestLocation() {
this.locationService()
}
private fun locationService() {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
val locationRequest = LocationRequest.create()
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
locationRequest.fastestInterval = 1000
locationRequest.interval = UPDATE_INTERVAL
locationRequest.maxWaitTime = MAX_WAIT_TIME
locationRequest.smallestDisplacement = 15f
val fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context)
fusedLocationProviderClient.lastLocation.addOnSuccessListener { location -> onLocationChanged(location) }
fusedLocationProviderClient.requestLocationUpdates(locationRequest, object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult) {
onLocationChanged(locationResult.lastLocation)
}
}, Looper.myLooper()!!)
}
}
override fun onLocationChanged(location: Location) {
try {
Log.v("IntermitentLocationThread", "onLocationChanged")
if (location != null) {
val commons = LocationCommons()
// if (!commons.isMock(context, location) && commons.isBetterLocation(location, bestLocation)) {
Log.v("IntermitentLocationThread", "isBetter true")
val prefs = Prefs(context)
prefs.latitude = location.latitude.toFloat()
prefs.longitude = location.longitude.toFloat()
prefs.precisao = location.accuracy
prefs.velocidade = location.speed * 3.6f
prefs.bearing = location.bearing
if (location.extras.containsKey("satellites")) {
prefs.satellites = location.extras.getInt("satellites")
}
GeoDecodeTask(context, location).decoder { }
bestLocation = location
onLocationUpdate(bestLocation)
} else {
Log.v("IntermitentLocationThread", "isBetter false")
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
下面是我在 Activity 中的实现方式:
private void handleLocationUpdates() {
if (isLocationEnabled()) {
loadMapScene();
IntermitentLocationThread thread = new IntermitentLocationThread(this, location -> {
Log.e(TAG, "handleLocationUpdates: "+"pegado localização" );
clearPassageiroMapMarker();
addPassageiroMarker(new GeoCoordinates(location.getLatitude(),
location.getLongitude()), R.drawable.ic_passageiro);
passageiro.setLat(location.getLatitude());
passageiro.setLongitude(location.getLongitude());
SharedPreferences.Editor editor = this.getSharedPreferences(Constantss.PREFERENCES, MODE_PRIVATE).edit();
mapView.getCamera().lookAt(new GeoCoordinates(passageiro.getLat(), passageiro.getLongitude()));
return null;
});
thread.requestLocation();
} else {
Toast.makeText(this, "Por favor" + "ative sua localização...", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
}
目前,我得到 passengers/drivers' 位置的位置非常奇怪和奇怪,它不像优步那样“流畅”。现在,我的标记从一个点跳到另一个点 (因为我清除了标记列表并设置了另一个,仍在努力在 heremaps 地图中只有一个标记) 在一个范围内5 到 15 秒,互联网上的其他人似乎都使用这个 google 引擎来跟踪一个人的位置。
如何以更快/更顺畅的方式跟踪用户的位置?
我能够使用 Google 的位置管理器找到 better/fast 跟踪...
是的.. LocationManager。
我之前使用的代码根本不准确,使用 Googles Location Manager 我能够设置特定的计时器并保持其准确。
更多关于:https://developer.android.com/reference/android/location/LocationManager
我已经完成了我正在开发的产品,但是目前,我们跟踪用户(乘客)位置以及司机的速度太慢了。 这是我用来在地图移动时使用乘客/司机图标跟踪和更新地图的代码:
import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.location.Location
import android.os.Looper
import android.util.Log
import androidx.core.content.ContextCompat
import com.google.android.gms.location.*
import mobi.audax.tupi.motorista.bin.task.GeoDecodeTask
import mobi.audax.tupi.passageiro.util.Prefs
class IntermitentLocationThread(val context: Context, val onLocationUpdate: (location: Location?) -> Unit) : LocationListener {
private var UPDATE_INTERVAL = (1000 * 10).toLong() // 10 segundos de intervalo
private val MAX_WAIT_TIME = UPDATE_INTERVAL * 2 // 20 segundos
private var bestLocation: Location? = null
fun requestLocation() {
this.locationService()
}
private fun locationService() {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
val locationRequest = LocationRequest.create()
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
locationRequest.fastestInterval = 1000
locationRequest.interval = UPDATE_INTERVAL
locationRequest.maxWaitTime = MAX_WAIT_TIME
locationRequest.smallestDisplacement = 15f
val fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context)
fusedLocationProviderClient.lastLocation.addOnSuccessListener { location -> onLocationChanged(location) }
fusedLocationProviderClient.requestLocationUpdates(locationRequest, object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult) {
onLocationChanged(locationResult.lastLocation)
}
}, Looper.myLooper()!!)
}
}
override fun onLocationChanged(location: Location) {
try {
Log.v("IntermitentLocationThread", "onLocationChanged")
if (location != null) {
val commons = LocationCommons()
// if (!commons.isMock(context, location) && commons.isBetterLocation(location, bestLocation)) {
Log.v("IntermitentLocationThread", "isBetter true")
val prefs = Prefs(context)
prefs.latitude = location.latitude.toFloat()
prefs.longitude = location.longitude.toFloat()
prefs.precisao = location.accuracy
prefs.velocidade = location.speed * 3.6f
prefs.bearing = location.bearing
if (location.extras.containsKey("satellites")) {
prefs.satellites = location.extras.getInt("satellites")
}
GeoDecodeTask(context, location).decoder { }
bestLocation = location
onLocationUpdate(bestLocation)
} else {
Log.v("IntermitentLocationThread", "isBetter false")
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
下面是我在 Activity 中的实现方式:
private void handleLocationUpdates() {
if (isLocationEnabled()) {
loadMapScene();
IntermitentLocationThread thread = new IntermitentLocationThread(this, location -> {
Log.e(TAG, "handleLocationUpdates: "+"pegado localização" );
clearPassageiroMapMarker();
addPassageiroMarker(new GeoCoordinates(location.getLatitude(),
location.getLongitude()), R.drawable.ic_passageiro);
passageiro.setLat(location.getLatitude());
passageiro.setLongitude(location.getLongitude());
SharedPreferences.Editor editor = this.getSharedPreferences(Constantss.PREFERENCES, MODE_PRIVATE).edit();
mapView.getCamera().lookAt(new GeoCoordinates(passageiro.getLat(), passageiro.getLongitude()));
return null;
});
thread.requestLocation();
} else {
Toast.makeText(this, "Por favor" + "ative sua localização...", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
}
目前,我得到 passengers/drivers' 位置的位置非常奇怪和奇怪,它不像优步那样“流畅”。现在,我的标记从一个点跳到另一个点 (因为我清除了标记列表并设置了另一个,仍在努力在 heremaps 地图中只有一个标记) 在一个范围内5 到 15 秒,互联网上的其他人似乎都使用这个 google 引擎来跟踪一个人的位置。
如何以更快/更顺畅的方式跟踪用户的位置?
我能够使用 Google 的位置管理器找到 better/fast 跟踪... 是的.. LocationManager。 我之前使用的代码根本不准确,使用 Googles Location Manager 我能够设置特定的计时器并保持其准确。 更多关于:https://developer.android.com/reference/android/location/LocationManager