Search code examples
androiddelphifiremonkey

Delphi Get LocationSensor Speed on Android


I'm currently developing a Localization Android App on Delphi / RADStudio Tokyo 10.2

I'm pretty new to delphi programming, and I am looking for a way to get the speed property of the LocationSensor. Through the documentation I found this:

LocationSensor1.Sensor.Speed 

But it always returns NaN, so I did some digging and found this in System.Sensors:

function TCustomLocationSensor.GetDoubleProperty(Prop: TProperty): Double;
begin
  Result := NaN;
end;

So I am confused, am I using the wrong property, or using it wrongly ? If so what should I use, or how should I use it?

EDIT

My question differs from Xe5 locationsensor distance doesn't work? as I have no problem with GPS precision (or at least it doesn't seem so), allthough they might not be unrelated.The application communicates with a Server, that will display it's position and it's trajectory on a web browser.

For optimal usage, I have to transmit the travelling speed of the device. I thought of using Accelerometer at first, but it didn't fit the situation. Then I stumbled upon this on the embarcadero documentation : http://docwiki.embarcadero.com/Libraries/Tokyo/en/System.Sensors.TCustomLocationSensor.Speed

This was precisely what I needed, but when i try to get the value it always returns NaN, as explained above. Whilst debugging, I finally found the code showed above in System.Sensors and I thought it was strange that the function always returns NaN no matter what happens, as I found no help online ( just a few websites in japanese or russian http://www.360doc.com/content/17/0502/18/9200790_650383719.shtml http://www.cyberforum.ru/delphi-beginners/thread1386722.html) I posted here.


Solution

  • Try to do not use standard firemonkey TLocationSensor. Instead of that try implement solution based on native java classes (JLocationListener).

      TAndroidLocationListener = class(TJavaLocal, JLocationListener)
      public
        constructor Create();
        procedure onLocationChanged(location: JLocation); cdecl;
        procedure onProviderDisabled(provider: JString); cdecl;
        procedure onProviderEnabled(provider: JString); cdecl;
        procedure onStatusChanged(provider: JString; status: Integer; extras: JBundle); cdecl;
      end;
    

    in onLocationChanged(location: JLocation); cdecl; you will get instance of current location object (java interface)

    For test purposes create notification event with message location.getSpeed.ToString()

    Here you got similar question How to track the user location at full time with a delphi app under android/ios