Reputation: 31
I am trying to make my location algorythm smarter by changing the minimum interval of location update depending on the current speed of the user. There is the problem with the following code, no mather of the speed i constantly get updates every 8 seconds. I would appriciate any help. Here is the main part of the code:
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");
}
}
}
Upvotes: 3
Views: 1112
Reputation: 174
Get speed from your GPS location as bellow:
int updateCount=0;
float totalSpeedSum=0.0f;
float avgSpeed=0.0f;
public void onLocationChanged(Location location) {
Location lastLocation = getLastLocation();
// speed in m/sec
float curSpeed=location.getSpeed();
updateCount++;
totalSpeedSum=totalSpeedSum+curSpeed;
avgSpeed=totalSpeedSum/updateCount;
}
Upvotes: 0
Reputation: 7332
I think that the problem is, that you call removeUpdates and requestLocationUpdates every time when onLocationChanged is called. When you call requestLocationUpdates
, GPS starts working immediately and tries to fix your location as soon as possible.
When the location is fixed, next fix will be performed after the delay you specified, but you don't allow that, because you restart the process by calling removeUpdates and requestLocationUpdates every time. I think that's why you always get GPS fix after a few seconds.
Upvotes: 1