![android | Embarcadero RAD Studio Delphi C++Builder Blogs](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2020/08/android-1.jpg?resize=1140%2C628&ssl=1)
Author: Michael Gillen
Sensor support was originally added to Delphi a year ago. The sensor APIs are defined in the System.Sensors unit name scope and can be use in your Windows VCL and FM apps. This blog post will show you how you can easily location enable your Delphi VCL Windows applications. You’ll need a GPS device for your Windows computer (some of the recent Intel based Ultrabooks have GPS hardware built in. On the Intel Ultrabook page they list additional built-in sensors: compass, accelerometer, inclinometer, Gyroscope and light sensor). Another option is to install a GPS driver that connects a real device via a COM port (I have a Garmin Nuvi) that I don’t use in my car anymore. A third option is to install a location driver that can “simulate” a GPS location and/or will use IP address location finding to give you a pretty close location result. I’ve also installed GPSDirect on my Samsung Slate Series 7 tablet running Windows 8.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
uses System.Sensors,System.TypInfo,Winapi.ShellAPI;<br> <br> const<br> GoogleMapsURL: String = 'https://maps.google.com/maps?q=%s,%s&output=embed';<br> <br> procedure TForm1.Button1Click(Sender: TObject);<br> var<br> MyLocationSensorArray : TSensorArray;<br> MyLocationSensor : TCustomLocationSensor;<br> begin<br> try<br> TSensorManager.Current.Activate; // activate sensor manager<br> MyLocationSensorArray := TSensorManager.Current.GetSensorsByCategory(TSensorCategory.Location);<br> if MyLocationSensorArray <> nil then begin<br> Label3.Caption := 'Location Sensor Found';<br> MyLocationSensor := MyLocationSensorArray[0] as TCustomLocationSensor;<br> MyLocationSensor.Start;<br> LatitudeLabel.Caption := FloatToStr(MyLocationSensor.Latitude);<br> LongitudeLabel.Caption := FloatToStr(MyLocationSensor.Longitude);<br> MyLocationSensor.Stop;<br> ShellExecute(handle,'open',PChar(Format(GoogleMapsURL,<br> [MyLocationSensor.Latitude.ToString, MyLocationSensor.Longitude.ToString])),<br> '','',SW_SHOWNORMAL);<br> end<br> else begin<br> Label3.Caption := 'Location Sensor Not Found!'<br> end;<br> finally<br> TSensorManager.Current.DeActivate // deactivate sensor manager<br> end;<br> 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.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition