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
Expert Delphi by Paweł Głowacki.mobi
Expert Delphi by Paweł Głowacki.pdf
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.
Hello! Python by Anthony Briggs(9916)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9796)
The Mikado Method by Ola Ellnestam Daniel Brolund(9779)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8298)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7778)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6847)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6580)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6450)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6414)
Kotlin in Action by Dmitry Jemerov(5064)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4317)
Functional Programming in JavaScript by Mantyla Dan(4038)
Solidity Programming Essentials by Ritesh Modi(4008)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3800)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3743)
The Ultimate iOS Interview Playbook by Avi Tsadok(3716)
