1

I have an application that tracks users location. My application uses FusedLocationApi , Google new way of getting device location using Google Play Services.

Provider of locations that i give from GPS is labeled as fused.

Because of application users may change device time, it is important to get real time from GPS.

Problem is that when i try to get time from Location object , it returns device time not GPS time.

Any solutions to get GPS time in this situation are appreciated.

private Location mLastLocation;    

public void onConnected(Bundle arg0) {

    LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);
}

public void onLocationChanged(Location location) {

    mLastLocation = LocationServices.FusedLocationApi
            .getLastLocation(mGoogleApiClient);

    if (mLastLocation != null) {

        double latitude = mLastLocation.getLatitude();
        double longitude = mLastLocation.getLongitude();
        Date date = new Date(mLastLocation.getTime());

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");            
        String time = dateFormat.format(date)
}
deuxdeux
  • 63
  • 9
  • You must disable using Network as location provider. – theWalker Feb 14 '16 at 10:39
  • see: http://stackoverflow.com/questions/4418344/android-problem-finding-out-how-recent-latest-gps-fix-is – theWalker Feb 14 '16 at 10:42
  • @skippy : Sorry for respond delay. You are right . This is possible with `LocationManager`, but how can i do this in `FusedLocationApi` ? Is this solution possible? – deuxdeux Feb 20 '16 at 11:31
  • tell me, why you use FuseLocatinApi? What is special in this for you? – theWalker Feb 20 '16 at 18:32
  • `FusedLocationApi` provides better accuracy with less battery drain. It's a 'fused' location provider, so it uses a mixture of GPS and network provided location to give you a trade-off between accuracy and battery consumption. It can also be updated independently of the OS, so it has a shorter release cycle for updates. see this video https://www.youtube.com/watch?v=Bte_GHuxUGc – deuxdeux Feb 21 '16 at 08:02
  • I always get the local time, not the gps time. Did you get the right time from GPS? – Rafael Leonhardt May 24 '16 at 20:14
  • @RafaelLeonhardt : With `LocationManager` it's possible to get right time from gps but i can't do it with `FusedLocationApi`. It's always returned device time instead of gps time. Instead of this approach i force my application users to check automatic time and zone in setting. – deuxdeux May 26 '16 at 03:51

1 Answers1

1

To get GPS UTC, do it in normal way:

LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
// not LocationManager.NETWORK_PROVIDER

@Override
public void onLocationChanged(Location location) 
{   
    long utc = location.getTime();
    // ....
}
theWalker
  • 2,022
  • 2
  • 18
  • 27
  • That's great, but as you mention this works in normal way ( `LocationManager` ) – deuxdeux Feb 21 '16 at 08:08
  • 1
    You can run this service occasionally, to count delta between GPS UTC and system time or time reported by FusedLocationApi – theWalker Feb 21 '16 at 08:13