gpt4 book ai didi

java - Android 位置间隔变化

转载 作者:行者123 更新时间:2023-12-04 18:04:49 24 4
gpt4 key购买 nike

我试图通过根据用户的当前速度更改位置更新的最小间隔来使我的位置算法更智能。以下代码存在问题,无论速度如何,我每 8 秒就会不断更新一次。我会 appriciate 任何帮助。这是代码的主要部分:

 public void onLocationChanged(Location location) {

Location lastLocation = getLastLocation();

if(lastLocation != null){

double speed=location.distanceTo(lastLocation)/((getMilSecFromDate(getCurrentDateTime())-lastLocation.getTime())/1000);
speedTmp = speed;

if(!isLocEqual(location,lastLocation)){
Toast.makeText(LocationService.this, speed+" "+"Location: " + "Time: " + getCurrentDateTime() + " Latitude: " + location.getLatitude() + " Longitude: "
+ location.getLongitude(), Toast.LENGTH_SHORT).show();

timeMoved=Calendar.getInstance().getTimeInMillis();
//if battery is under 20%, service is shutted down
if(batPercent<20){
onDestroy();
Toast.makeText(LocationService.this, "Battery under 20%, charge", Toast.LENGTH_SHORT).show();
}

if(speed<=10&&speed>2){
Toast.makeText(LocationService.this, "Hodanje - speed " +speed, Toast.LENGTH_LONG).show();
MIN_TIME=1000*60;
locationManager.removeUpdates(locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, 0, locationListener);
Log.i("gpsSetvice", "speed<10");
}
else if(speed>10 && speed<60){
Toast.makeText(LocationService.this, "Izmedju 10 i 60 - speed " +speed, Toast.LENGTH_LONG).show();
MIN_TIME=1000*30;
locationManager.removeUpdates(locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, 0, locationListener);
Log.i("gpsSetvice", "speed>10 && speed<60");
}
else if(speed>=60 && speed<100){
Toast.makeText(LocationService.this, "izmedju 60 i 100 " +speed, Toast.LENGTH_LONG).show();
MIN_TIME=1000*20;
locationManager.removeUpdates(locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, 0, locationListener);
Log.i("gpsSetvice", "speed>=60 && speed<100");
}
else if(speed>=100){
Toast.makeText(LocationService.this, "Preko 100 - speed " +speed, Toast.LENGTH_LONG).show();
MIN_TIME=1000*2*60;
locationManager.removeUpdates(locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, 0, locationListener);
Log.i("gpsSetvice", "speed>=MILE");
}

}
}

最佳答案

我认为问题在于,每次调用 onLocationChanged 时,您都会调用 removeUpdates 和 requestLocationUpdates。当您调用 requestLocationUpdates 时,GPS 会立即开始工作并尝试尽快确定您的位置。

当位置固定时,下一次修复将在您指定的延迟后执行,但您不允许这样做,因为您每次都通过调用 removeUpdates 和 requestLocationUpdates 来重新启动该过程。我认为这就是为什么您总是会在几秒钟后获得 GPS 定位。

关于java - Android 位置间隔变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28965802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com