我正在开发一个具有跟踪GPS位置以绘制地图的功能的应用程序。
基本上,每秒我将一个点(包含纬度和经度以及其他信息)保存到数组中,然后每15秒将数组保存到数据库中一次,并将所有信息发送到服务器一次旅行结束了。
我每秒使用Location.getLatitude()和Location.getLongitude()获取位置。但是,在旅行后查看发送到服务器的文件时,经度和纬度仅每100到300秒更改一次(在汽车中记录,因此应该更频繁地更改)-在9公里的旅行中,花了24分钟,我只记录了8个不同的纬度/经度组合(包括开始和结束)
我正在记录的其他点正在每秒更新一次,因此这纯粹是经度/纬度问题。
是否需要做一些操作才能使location.getLongitude()更新得更频繁?或者另外一种获取经度/纬度的方法会更好
谢谢
答案 0 :(得分:1)
创建位置请求时,请将间隔和最快间隔设置为较小的数字。像这样:
private val UPDATE_INTERVAL = (30 * 1000).toLong() /* 30 secs */
private val FASTEST_INTERVAL: Long = 10000 /* 10 sec */
然后创建您的locationRequest:
locationRequest = LocationRequest.create()
locationRequest.interval = UPDATE_INTERVAL
locationRequest.fastestInterval = FASTEST_INTERVAL
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
答案 1 :(得分:0)
我正在使用myLocationManager。
您可以更改所需的 UPDATE_INTERVAL_IN_MILLISECONDS 属性
并观察mLocationCalback的位置事件。
class MyLocationManager constructor( var context: Context ) {
private var fusedLocationClient: FusedLocationProviderClient? = null
var locationStatus = BehaviorSubject.create<LOCATION_STATUS>()
/**
* Provides access to the Location Settings API.
*/
var mSettingsClient: SettingsClient? = null
/**
* Stores parameters for requests to the FusedLocationProviderApi.
*/
private var mLocationRequest: LocationRequest? = null
/**
* Callback for Location events.
*/
var mLocationCallback: LocationCallback? = null
/**
* Stores the types of location services the client is interested in using. Used for checking
* settings to determine if the device has optimal location settings.
*/
private var mLocationSettingsRequest: LocationSettingsRequest? = null
/**
* Constant used in the location settings dialog.
*/
private val REQUEST_CHECK_SETTINGS = 0x1
/**
* The desired interval for location updates. Inexact. Updates may be more or less frequent.
*/
private val UPDATE_INTERVAL_IN_MILLISECONDS: Long = 60000
private val FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2
init {
initializeLocationManager()
}
private fun initializeLocationManager() {
if (mSettingsClient == null) {
mSettingsClient = LocationServices.getSettingsClient(context)
}
if (fusedLocationClient == null) {
fusedLocationClient = LocationServices.getFusedLocationProviderClient(context)
}
createLocationServices()
}
private fun createLocationServices() {
createLocationCallback()
createLocationRequest()
buildLocationSettingsRequest()
}
companion object {
enum class LOCATION_STATUS {
REQUIRE_ACCESS_FINE_LOCATION
}
}
private fun createLocationRequest() {
mLocationRequest = LocationRequest()
// Sets the desired interval for active location updates. This interval is
// inexact. You may not receive updates at all if no location sources are available, or
// you may receive them slower than requested. You may also receive updates faster than
// requested if other applications are requesting location at a faster interval.
mLocationRequest?.interval = UPDATE_INTERVAL_IN_MILLISECONDS
// Sets the fastest rate for active location updates. This interval is exact, and your
// application will never receive updates faster than this value.
mLocationRequest?.fastestInterval = FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS
mLocationRequest?.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}
private fun buildLocationSettingsRequest() {
val builder = LocationSettingsRequest.Builder()
mLocationRequest?.let { builder.addLocationRequest(it) }
mLocationSettingsRequest = builder.build()
}
/**
* Requests location updates from the FusedLocationApi. Note: we don't call this unless location
* runtime permission has been granted.
*/
private fun startLocationUpdates(activity: Activity, justControl: Boolean = false) {
// Begin by checking if the device has the necessary location settings.
if (ContextCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_FINE_LOCATION
)
== PackageManager.PERMISSION_GRANTED
) {
mSettingsClient?.let {
it.checkLocationSettings(mLocationSettingsRequest)
.addOnSuccessListener(activity) {
fusedLocationClient?.requestLocationUpdates(
mLocationRequest,
mLocationCallback, Looper.myLooper()
)
}.addOnFailureListener(activity) { e ->
val statusCode = (e as ApiException).statusCode
when (statusCode) {
LocationSettingsStatusCodes.RESOLUTION_REQUIRED -> {
try {
// Show the dialog by calling startResolutionForResult(), and check the
// result in onActivityResult().
if (!justControl) {
val rae = e as ResolvableApiException
rae.startResolutionForResult(
activity,
REQUEST_CHECK_SETTINGS
)
}
} catch (sie: IntentSender.SendIntentException) {
}
}
LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE -> {
val errorMessage =
"Location settings are inadequate, and cannot be " + "fixed here. Fix in Settings."
}
}
}
}
}
}
/**
* Creates a callback for receiving location events.
*/
private fun createLocationCallback() {
mLocationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) {
super.onLocationResult(locationResult)
locationResult?.lastLocation?.let {
// fetched location
}
}
}
}
fun getLastKnowLocation(activity: Activity, justControl: Boolean = false) {
if (ContextCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED
) {
fusedLocationClient?.let {
it.lastLocation
.addOnSuccessListener { location: Location? ->
location?.let {
// fetched last know location
} ?: startLocationUpdates(activity, justControl)
}
}
} else {
locationStatus.onNext(LOCATION_STATUS.REQUIRE_ACCESS_FINE_LOCATION)
}
}
private fun stopLocationUpdates() {
fusedLocationClient?.removeLocationUpdates(mLocationCallback)
}
}
然后添加权限
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />