Expert Delphi by Paweł Głowacki

Expert Delphi by Paweł Głowacki

Author:Paweł Głowacki
Language: eng
Format: epub, mobi, pdf
Tags: COM051010 - COMPUTERS / Programming Languages / General, COM051070 - COMPUTERS / Programming Languages / C++, COM060080 - COMPUTERS / Web / General
Publisher: Packt
Published: 2017-07-28T11:05:21+00:00


Sensor components on the tool palette

A lot of mobile apps depend on location information. The GPS sensor is a standard part of a typical smartphone or tablet. With the TLocationSensor component, it is very easy to add location awareness to an app.

There is nothing like building a test app and giving sensor components a try! Create a new multi-device Delphi app, select a Blank Application template, and then save the main form's unit as uFormSensors and the project as Sensors. You can also change the Name property of the form to FormSensors. Locate in the tool palette the Sensors category, or just use IDE Insight, and add TLocationSensor to the form. First of all, we need to switch the sensor on. There is an Active property that needs to be set to True. Drop the TCheckBox component on the form and align it to the Top. Change all its Margin subproperties to 8, so there is a distance from the border of the form. Change its Name to chkbxLocSensor and its Text property to Location Sensor Active. In its OnChange event, enter the following line of code:

procedure TFormLoc.chkbxLocSensorChange(Sender: TObject);

begin

LocationSensor1.Active := chkbxLocSensor.IsChecked;

end;

The next step is to respond to location change events. Different readings from sensors are typically real numbers, so before proceeding, let's implement a small convenience function DblToStr that will be converting float numbers to strings to avoid calling just ToString methods that would display 14 digits after the decimal point. We just need a few:

uses System.Math; // IsNan

// ...

function TFormSensors.DblToStr(Value: Double): string;

begin

if not IsNan(Value) then

Result := Format('%3.5f',[Value])

else

Result := 'NaN';

end;

Add the TLabel component to the form. Align it to the Top and change its Name to lblLocation. Expand its TextSettings property in the Object Inspector and change the HorzAlign property to Center. Change the Text property to (Location). Double-click on the OnLocationChange event of the location sensor and enter the following code to display the current geographical coordinates read from the GPS of our device:

procedure TFormLoc.LocationSensor1LocationChanged(Sender: TObject;

const OldLocation, NewLocation: TLocationCoord2D);

begin

lblLocation.Text := DblToStr(NewLocation.Latitude)

+ ' : ' + DblToStr(NewLocation.Longitude);

end;

Save all and run the app. First, on iOS. From the moment that we touch the checkbox to enable the location sensor, we should see the confirmation dialog from the iOS operating system asking if we want to allow our app to have access to location information.

Click on Allow and you should see the current latitude and longitude in the label.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.