0

I want to get the speed out of the LocationManager in my app. I have a Criteria with setSpeedRequired(true) attribute. I am doing location.getSPeed(), but it gives me 0 all the time. Below is the code for the GPS which I am running as a Service.

public class Tracking extends Service implements LocationListener {

    protected LocationManager locationManager;
    Location location;
    double latitude;
    double longitude;
    float velocity;
    String provider;

    private static final long minDist = 0;
    private static final long minTime = 0;
    LocationDatabaseHandler ldb;


    @Override
    public void onCreate() {
        ldb = new LocationDatabaseHandler(this);
        getLocation();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent,flags,startId);
        //because we do not want to stop the service unless we explicitly say so.
        return START_STICKY;
    }

    public Location getLocation() {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setSpeedRequired(true);
        provider = locationManager.getBestProvider(criteria, false);

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDist,
                this);
        location = locationManager.getLastKnownLocation(provider);
        if(location != null) {
            onLocationChanged(location);            
        }

        return location;        
    }

    @Override
    public void onLocationChanged(Location location) {
        latitude = location.getLatitude();
        longitude = location.getLongitude();
        batteryP = getBatteryPerc();
        velocity = location.getSpeed();

        insertIntoDb(latitude, longitude, velocity, "onchanged");
    }

    public void insertIntoDb(double latitude, double longitude, float velocity, String where) {
        Date date = new Date();
        String dateStr = date.toString();

        ldb.addLocation(new Locations(latitude, longitude, dateStr, velocity));
    }

}

When I see my database, the velocity is always 0.0. Is there something I am missing?

Intern
  • 327
  • 1
  • 7
  • 23
  • Your `getSpeed()` code looks right to me. Are you certain you have a GPS lock? `getSpeed()` will always return 0 if it doesn't have a speed available to return. – Bryan Denny Apr 17 '13 at 19:57
  • You can check and see what the value of `location.getAccuracy()`. You may also want to look into using a GpsStatus.Listener to see what your current GPS status is (how many satellites you're connected to, if you have a GPS lock, etc) – Bryan Denny Apr 17 '13 at 20:01
  • Take a look here for more info on what I'm talking about http://stackoverflow.com/questions/2021176/how-can-i-check-the-current-status-of-the-gps-receiver/3712727#3712727 This was very helpful when I initially wrote Car Dashboard – Bryan Denny Apr 17 '13 at 20:02

1 Answers1

-2

locationManager.requestLocationUpdates(provider, minTime, minDist, this);