Using my demo VCL Sensor listing application (a blog post from last year), I got the following list of sensors found on my Samsung Series 7 Slate:
Notice that GPSDirect Sensor appears in the list of sensors found for TSensorCategory.Location.
Delphi's System.Sensors unit includes definitions for the following categories of sensors (TSensorCategory):
- Location
- Environmental
- Motion
- Orientation
- Mechanical
- Electrical
- Biometric
- Light
- Scanner
For each sensor category the unit defines (currently) the following possible sensor types:
- TLocationSensorType = (GPS, Static, Lookup, Triangulation, Broadcast, DeadReckoning, Other);
- TEnvironmentalSensorType = (Temperature, AtmosphericPressure, Humidity, WindSpeed, WindDirection);
- TMotionSensorType = (Accelerometer1D, Accelerometer2D, Accelerometer3D, MotionDetector, Gyrometer1D, Gyrometer2D, Gyrometer3D, Speedometer);
- TOrientationSensorType = (Compass1D, Compass2D, Compass3D, Inclinometer1D, Inclinometer2D, Inclinometer3D, Distance1D, Distance2D, Distance3D);
- TElectricalSensorType = (Voltage, Current, Capacitance, Resistance, Inductance, ElectricalPower, Potentiometer);
- TMechanicalSensorType = (BooleanSwitch, BooleanSwitchArray, MultiValueSwitch, Force, Scale, Pressure, Strain);
- TBiometricSensorType = (HumanPresence, HumanProximity, Touch);
- TLightSensorType = (AmbientLight); (Note: I showed how to use the AmbientLight sensor in your VCL apps at http://blogs.embarcadero.com/davidi/2013/12/02/43032)
- TScannerSensorType = (RFID, Barcode);
To build a simple VCL location enabled application I start by creating a new VCL Delphi application project. I added a TButton and five TLabel components to my form. Three of the labels will contain a) text declaring that a location sensor was found or not found, b) the latitude and longitude of my current location if a location sensor is found. Here is what my form looks like:
For the Button OnClick event handler I added the following code to get the location sensor using the TSensorManager GetSensorsByCategory method. GetSensorsByCategory takes a TSensorCategory as a parameter and returns a TSensorArray as a result. Using the method's result I instantiate a TCustomLocationSensor and get the latitude and longitude floating point values. Finally the OnClick event code uses ShellExecute to open a window and display a Google Map using the location coordinates. Here is the code for my button event handler:
uses System.Sensors,System.TypInfo,Winapi.ShellAPI;
const
GoogleMapsURL: String = 'https://maps.google.com/maps?q=%s,%s&output=embed';
procedure TForm1.Button1Click(Sender: TObject);
var
MyLocationSensorArray : TSensorArray;
MyLocationSensor : TCustomLocationSensor;
begin
try
TSensorManager.Current.Activate; // activate sensor manager
MyLocationSensorArray := TSensorManager.Current.GetSensorsByCategory(TSensorCategory.Location);
if MyLocationSensorArray <> nil then begin
Label3.Caption := 'Location Sensor Found';
MyLocationSensor := MyLocationSensorArray[0] as TCustomLocationSensor;
MyLocationSensor.Start;
LatitudeLabel.Caption := FloatToStr(MyLocationSensor.Latitude);
LongitudeLabel.Caption := FloatToStr(MyLocationSensor.Longitude);
MyLocationSensor.Stop;
ShellExecute(handle,'open',PChar(Format(GoogleMapsURL,
[MyLocationSensor.Latitude.ToString, MyLocationSensor.Longitude.ToString])),
'','',SW_SHOWNORMAL);
end
else begin
Label3.Caption := 'Location Sensor Not Found!'
end;
finally
TSensorManager.Current.DeActivate // deactivate sensor manager
end;
end;
When compiled and run using Delphi XE5 on my Samsung Series 7 Slate (I also testing it on my Dell desktop at home and my Dell notebook at work), I get the following program displays here in our Scotts Valley office:
I also get a Windows notification that my application has used the location API. Here is a snapshot of my Windows Event Viewer:
Its simple to location enable your Windows apps using VCL. Of course it would be even easier when I have a TLocationSensor non-visual component that works with my VCL apps. Stay tuned for a future post, unless someone else beats me to it.
Special Offer to all Delphi, C++Builder and RAD Studio customers
Buy RAD Studio, Delphi or C++Builder version XE5 at the low upgrade price if you have any earlier version of Delphi, C++Builder, RAD Studio or Borland Developer Studio. You will save up to 45% off the new user price and enjoy the full power of Delphi for Windows, OSX, iOS 7 and Android (in Delphi and RAD Studio). The offer is good until December 31, 2013. You'll find the RAD offer details, and the special promotions that go along with it, at http://www.embarcadero.com/radoffer.