Android - Locations got from LocationClient doesn't support speed
Asked Answered
I

1

2

I'm using the new Google LocationClient to retrieve geo locations. And I need to get speed for each point (location).
What I'm doing now is:

if (mLocationClient == null) {
    mLocationClient = new LocationClient(this, mLocationCallback, mLocationCallback);
    mLocationCallback.setLocationClient(mLocationClient);
    if (!(mLocationClient.isConnected() || mLocationClient.isConnecting())) {
        mLocationClient.connect();
    }
}

Where mLocationCallback is an instance of

public class LocationCallback implements ConnectionCallbacks,
        OnConnectionFailedListener, LocationListener {}

In the function,

    @Override
    public void onLocationChanged(Location location) {
        if (location == null) {
            Log.e(TAG, "onLocationChanged: location == null");
            return;
        }

        Log.i(TAG, "Location@ " + location.getLatitude() + ","
                   + location.getLongitude() + ", Altitude: "
                   + location.getAltitude() + "(" + location.hasAltitude()
                   + ")" + " Velocity: " + location.getSpeed() + " m/s("
                   + location.hasSpeed() + ")");
        }
}

However, everytime location.hasSpeed() gives false. It seems that only GPS provider gives speed. I'm sure my GPS is on but it might never be used. Is there a way to force LocationClient to use GPS provider?

Interscholastic answered 21/6, 2013 at 15:30 Comment(0)
I
3

Is there a way to force LocationClient to use GPS provider?

No, because the point of LocationClient is for it to blend data from multiple sources (GPS, WiFi, cell towers, sensors, etc.).

If you need speed, use LocationManager and work with the GPS_PROVIDER, or anything else the Criteria says supports speed (e.g., Galileo, someday, maybe).

Infusionism answered 21/6, 2013 at 15:35 Comment(3)
Yes you're right. Luckily I only need to collect points at a fixed rate. So I ended up calculating the distance between 2 adjacent locations divided by time interval. :)Interscholastic
Just a note on @XiangxinSun's method - when LocationClient reports a location without Speed (e.g. from WiFi) at a high frequency, the LatLng from one point to the next may not change, so your speed may end up being calculated out at 0 anyway.Amylose
@Infusionism LocationClient is deprecated. Look at this answer for current options: LocationManager or Location Service APIMackey

© 2022 - 2024 — McMap. All rights reserved.