why getSpeed() always return 0 on android

location getspeed android
calculate speed using gps android
calculate device moving speed programmatically using android
location gettime
location getaltitude
android location distanceto
location getaccuracy
location isfrommockprovider

I need to get the speed and heading from the gps. However the only number i have from location.getSpeed() is 0 or sometimes not available. my code:

        String provider = initLocManager();
    if (provider == null)
        return false;
    LocationListener locListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            updateWithNewLocation(location, interval, startId);
            Log.i(getString(R.string.logging_tag), "speed =" + location.getSpeed());

        public void onProviderDisabled(String provider){
            updateWithNewLocation(null, interval, startId);

        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}

    _locManager.requestLocationUpdates(provider, interval,  DEFAULT_GPS_MIN_DISTANCE, locListener);

    private String initLocManager() {
    String context = Context.LOCATION_SERVICE;
    _locManager = (LocationManager) getSystemService(context);

    Criteria criteria = new Criteria();
    String provider = _locManager.getBestProvider(criteria, true);

    if (provider == null || provider.equals("")) {
        return null;

    return provider;

I tried to play the Criteria with but no success. Does anyone have an idea what is the problem?

location.getSpeed() only returns what was set with location.setSpeed(). This is a value that you can set for a location object.

To calculate the speed using GPS, you'll have to do a little math:

Speed = distance / time

So you would need to do:

(currentGPSPoint - lastGPSPoint) / (time between GPS points)

All converted to ft/sec, or however you want to show the speed. This is how I did it when I made a runner app.

More specifically, you'll need to calculate for absolute distances:

(sqrt((currentGPSPointX - lastGPSPointX)^2) + (currentGPSPointY - lastGPSPointY)^2)) / (time between GPS points)

It might help to make a new TrackPoint class or something, which keeps the GPS location and time it was taken inside.

why location.getSpeed() always returns 0 while using , getSpeed() then it gives correct location Where as Network provider does not gives accurate location it gives location within 100 meter so when you call location. getSpeed() then it does not know where you are accurate between 100 meter. In NETWORK_PROVIDER hasSpeed() return false, so your location. getSpeed()=0. It returns 0 because your device cannot get a lock on the GPS, or cannot connect to the GPS. I tried to get the speed using an older lenovo device and it returns 0 because it cannot lock on a gps. I tried using a samsung galaxy nexus and it returned my speed(has a better GPS sensor).

There is my custom LocationListener used to get speed manually and by location object if has speed.

 new LocationListener() {
        private Location mLastLocation;

        public void onLocationChanged(Location pCurrentLocation) {
            //calcul manually speed
            double speed = 0;
            if (this.mLastLocation != null)
                speed = Math.sqrt(
                        Math.pow(pCurrentLocation.getLongitude() - mLastLocation.getLongitude(), 2)
                                + Math.pow(pCurrentLocation.getLatitude() - mLastLocation.getLatitude(), 2)
                ) / (pCurrentLocation.getTime() - this.mLastLocation.getTime());
            //if there is speed from location
            if (pCurrentLocation.hasSpeed())
                //get location speed
                speed = pCurrentLocation.getSpeed();
            this.mLastLocation = pCurrentLocation;
            //DO WHAT YOU WANT WITH speed VARIABLE

        public void onStatusChanged(String s, int i, Bundle bundle) {


        public void onProviderEnabled(String s) {


        public void onProviderDisabled(String s) {


why getSpeed() always return 0 on android, why getSpeed() always return 0 on android. I need to get the speed and heading from the gps. However the only number i have from location.getSpeed() is 0 or  why getSpeed() always return 0 on android (7) (1) I believe you can use the requestLocationUpdates() method and then create a LocationListener class with an onLocationChange method set to display getSpeed() .

Imbru's answer looks really good, but it is not very helpful if you are working with units.

Here's what I did to calculate the speed in meters per second (m/s).

new LocationListener() {
    private Location lastLocation = null;
    private double calculatedSpeed = 0;

    public synchronized void onLocationChanged(Location location) {
        if (lastLocation != null) {
            double elapsedTime = (location.getTime() - lastLocation.getTime()) / 1_000; // Convert milliseconds to seconds
            calculatedSpeed = lastLocation.distanceTo(location) / elapsedTime;
        this.lastLocation = location;

        double speed = location.hasSpeed() ? location.getSpeed() : calculatedSpeed;

        /* There you have it, a speed value in m/s */

        . . .


    . . .


why getSpeed() always return 0 on android, getSpeed() is 0 or sometimes not available. my code: String provider = initLocManager(); if (provider == null) return false; LocationListener locListener = new  But other sources seem to indicate that getSpeed should actually return a speed. The android docs say that getSpeed "Returns the speed of the device over ground in meters/second.". Other sources too seem to say that it works to return the speed of the device, e.g.

On a spherical planet distance should be calculated with these formulas :

private static Double distance(Location one, Location two) {
       int R = 6371000;        
       Double dLat = toRad(two.getLatitude() - one.getLatitude());
       Double dLon = toRad(two.getLongitude() - one.getLongitude());
       Double lat1 = toRad(one.getLatitude());
       Double lat2 = toRad(two.getLatitude());         
       Double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
               + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);        
       Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));        
       Double d = R * c;
       return d;
private static double toRad(Double d) {
       return d * Math.PI / 180;

android.location.Location.getSpeed java code examples, Returns the current speed * * @return the current speed (if detected) or `0` */ public float getSpeed() { if (mPosition == null) { return 0.0f; } else { return mPosition. You have to track location Updates, and when the method onLocationChanged(Location location) triggers, you can call location.getSpeed(); it will give you correct speed, if your phone is actually moving. But if you are testing it on Simulator, and sending location by emulator controller, it will always return 0. Updated with Example

(1) I believe you can use the requestLocationUpdates() method and then create a LocationListener class with an onLocationChange method set to display getSpeed(). This is how i recently saw it done with Location.getLatitude and Location.getLongitude, so I believe you could just use getSpeed() the same way, correct?

(2) After just reading the eclipse description window, though, I see it says exactly what the previous person said: "if hasSpeed() is false, 0.0f is returned." But maybe this will help: http://www.ehow.com/how_5708473_convert-latitude-feet.html :)

Location, Returns the approximate distance in meters between this location and the By default time, latitude and longitude are 0, and the location has no For example, if getSpeed() returns 5, and getSpeedAccuracyMetersPerSecond() So always use getElapsedRealtimeNanos() when calculating time deltas. I did discover that if I call this within a tap event (or any other event seconds after app launch like a setTimeout for that matter) that it will return an actual number. After some research it seems like a parent's loaded function is called before its children are initiated which leads to the 0 when called then.

Java Code Examples android.location.Location.getSpeed, This page provides Java code examples for android.location.Location. Location.getSpeed() @return a {@link Point} * @since 0.2.0 */ private static Point  4 Answers 4. GPS works fine in a vehicle. The NETWORK_PROVIDER setting might not be accurate enough to get a reliable speed, and the locations from the NETWORK_PROVIDER may not even contain a speed. You can check that with location.hasSpeed() (location.getSpeed() will always return 0).

How to Track Users Speed Using location.getSpeed() GPS Method , getSpeed() GPS Method in Android 0; 1; 0; 5.81k printStackTrace();; }; return location;; }; @Override; public void onLocationChanged(Location location) {; if  In every C program you have to use return return 0; (or return -1;, or whatever ), because the main function signature requires it. In a C++ program the statement is optional: the compiler automatically adds a return 0; if you don't explicitely return a value. The return value is the exit code of your program,

location/java/android/location/Location.java, <p>By default time, latitude and longitude are 0, and the location So always use getElapsedRealtimeUncertaintyNanos() returns 1000000 (equivalent to range above and below the estimated speed reported by {@link #getSpeed()},. Anyone have any idea why the speed would always return zero on some devices. It seems like it's AT&T devices and/or older devices. All the Verizon devices worked except one iPhone 6 we tested. We have downloaded the sample application and loaded it without modifications and it has the same problem as our implementation.

  • it will return distance in meter or kilometer?
  • I have seen issues with getSpeed() returning zero, even though 'hasSpeed()' is true and there has been motion. On one 6.0.1 device it always returns zero! From the Fused Location service. The regular LocationManager provides speeds, but seem less accurate and less frequent in its updates. So I went with my own speed calculation as suggested here.
  • i used your code it works like this it retruns speed as 2.25 but my vehicle speed is about 60km/h .How to calculate the movement speed exactly?
  • @Kevin ABRIOUX what is the unit of calculated speed. is it km/hr ?
  • The speed calculation should be done only if location.hasSpeed()==false. Some people also reported that location.getSpeed() always return 0 even if location.hasSpeed()==true. Thus, I would use location.hasSpeed() && location.getSpeed()>0 as a condition.
  • what if the user is on a slope then??
  • I don't think this is true, I do have longitude and latitude but getSpeed() return always 0.0
  • Where does this 18/5 magic number come from? Also, how does this help the problem of " getSpeed() returns 0 " ?
  • getSpeed() method returns the value in m/s which returns always zero so convert into km/h by using 18/5 1 m/s=18/5 Km/hr