我试图通过改变用户当前速度来使我的位置算法更加智能化,从而改变位置更新的最小间隔。但以下代码存在问题,无论速度如何,我都会不断地在每8秒内更新。希望得到任何帮助。
以下是主要代码部分: 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");
}
}
}