Home How can I get the current date and time from the GPS sensor in FireMonkey for Android?
Reply: 2

How can I get the current date and time from the GPS sensor in FireMonkey for Android?

roozgar
1#
roozgar Published in 2017-09-12 19:51:19Z

How can I get the current date time from GPS/glonas/baido satellites on my mobile device with Delphi Android programming?

At this time I can just get longitude and latitude from the location sensor.

procedure TForm1.LocationSensor1LocationChanged(Sender: TObject;
  const OldLocation, NewLocation: TLocationCoord2D);
var
  LDecSeparator: String;
begin
  LDecSeparator := FormatSettings.DecimalSeparator;
  FormatSettings.DecimalSeparator := '.';
  // Show current location
  ListBoxItemLatitude.ItemData.Detail  := Format('%2.6f', [NewLocation.Latitude]);
  ListBoxItemLongitude.ItemData.Detail := Format('%2.6f', [NewLocation.Longitude]);
end;

I know it's easier to use GSM network for time but there is no mobile network available on my case.

Is this possible at all?

peiman F.
2#
peiman F. Reply to 2017-09-13 04:36:00Z

when you connect location sensor from the provided framware it just provide you location data

but you can get raw data and grab time data

FYI

because the handset companies using chap gps hardware's on their product,the output data have low accuracy

for example data is sampling in 1Hz rate so the best value you get is accuracy 1 second:

use this code:

    procedure TForm1.Button1Click(Sender: TObject);
    var
      LocationManagerService: JObject;
      location : JLocation;
    begin
      if not Assigned(FLocationManager) then
      begin
        LocationManagerService := SharedActivityContext.getSystemService(TJContext.JavaClass.LOCATION_SERVICE);
        FLocationManager := TJLocationManager.Wrap((LocationManagerService as ILocalObject).GetObjectID);
        if not Assigned(locationListener) then
          locationListener := TLocationListener.Create(self);
        FLocationManager.requestLocationUpdates(TJLocationManager.JavaClass.GPS_PROVIDER, 10000, 10, locationListener,
            TJLooper.JavaClass.getMainLooper);
      end;                      

      location := FLocationManager.getLastKnownLocation(TJLocationManager.JavaClass.GPS_PROVIDER);
  Memo1.Lines.Add(location.getTime.ToString);
    end;

this output is like 1505216957000 time stamp , but for low accuracy the last 3 digit is always 0 so you must remove 000 from last of number and then convert it to your time zone

Victoria
3#
Victoria Reply to 2017-09-13 06:33:41Z

This should give you UTC timestamp of the last received position:

procedure TForm1.LocationSensor1LocationChanged(Sender: TObject; const OldLocation,
  NewLocation: TLocationCoord2D);
var
  LastPosTime: TDateTime;
begin
  LastPosTime := TLocationSensor(Sender).Sensor.TimeStamp;
  ...
end;

On Android platform the TimeStamp property getter calls the getTime method of the last received location sample.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.492459 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO