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?

xiangxin
  • 409
  • 6
  • 18

1 Answers1

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).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 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. :) – xiangxin Jun 26 '13 at 11:23
  • 1
    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. – warbi Feb 13 '14 at 12:28
  • @CommonsWare [LocationClient](https://developer.android.com/reference/com/google/android/gms/location/LocationClient.html) is deprecated. Look at this answer for current options: [LocationManager or Location Service API](http://stackoverflow.com/questions/24266362/android-google-maps-api-location/27195920#27195920) – MiguelHincapieC Nov 28 '14 at 21:43