.Net Runtime Library for Delphi
CNClrLib.Device Namespace

The CNClrLib.Device namespace allows application developers to easily access the computer's location by using a single API. Location information may come from multiple providers, such as GPS, Wi-Fi triangulation, and cell phone tower triangulation. The CNClrLib.Device interfaces provide a single API to encapsulate the multiple location providers on a computer and support seamless prioritization and transitioning between them. As a result, application developers who use this API do not need to tailor applications to specific hardware configurations. These interfaces are defined as classes and interfaces in C# System.Device.Location Namespace.


The following example shows how to handle StatusChanged events and print out the current GeoPositionStatus.

program DeviceLocationExample; {$APPTYPE CONSOLE} {$R *.res} uses SysUtils, CNClrLib.EnumArrays, CNClrLib.EnumTypes, CNClrLib.Device, CNClrLib.Host, CNClrLib.Host.Helper, CNClrLib.Host.Utils, CNClrLib.Core; var Console: _Console; // Event Handler in C# looks like this: // static void DoStatusChanged(object sender, GeoPositionStatusChangedEventArgs e) procedure DoStatusChanged(ASender: _ClrObject; AEventArgs: _ClrEventArgs); stdcall; var e: _GeoPositionStatusChangedEventArgs; ageoPositionStatus: TGeoPositionStatus; begin //Convert EventArgs value to GeoPositionStatusChangedEventArgs e := CoGeoPositionStatusChangedEventArgs.Wrap(AEventArgs.EventArgs); //Convert the status as TOleEnum to a delphi readable enumration type defined in CNClrLib.EnumTypes ageoPositionStatus := TGeoPositionStatus(OleEnumToOrd(GeoPositionStatusValues, e.Status)); case ageoPositionStatus of gpsInitializing: Console.WriteLine_14('Working on location fix'); gpsReady: Console.WriteLine_14('Have location'); gpsNoData: Console.WriteLine_14('No data'); gpsDisabled: Console.WriteLine_14('Disabled'); end; end; procedure ShowStatusUpdates; var watcher: _GeoCoordinateWatcher; eventHandler: TClrEventHandler; begin watcher := CoGeoCoordinateWatcher.CreateInstance; watcher.Start; eventHandler := DoStatusChanged; watcher.Add_StatusChanged(TClrIntPtrHelper.Zero, TClrConvert.ToManagedPointer(@eventHandler)); Console.WriteLine_14('Enter any key to quit.'); Console.ReadLine; end; begin try Console := CoConsole.CreateInstance; ShowStatusUpdates; except on E: Exception do begin Console.WriteLine_14(E.message); end; end; end.

ExamplesThe following example shows how to resolve a CivicAddress from a GeoCoordinate location synchronously.
program ResolveAddressSync; {$APPTYPE CONSOLE} {$R *.res} uses SysUtils, CNClrLib.EnumTypes, CNClrLib.Device, CNClrLib.Core; var Console: _Console; procedure ResolveAddressSync; var watcher: _GeoCoordinateWatcher; resolver: _CivicAddressResolver; location: _GeoCoordinate; timespan: _TimeSpanHelper; address: _CivicAddress; begin watcher := CoGeoCoordinateWatcher.CreateInstance(gpaHigh); watcher.MovementThreshold := 1.0; // set to one meter timespan := CoTimeSpanHelper.CreateInstance; watcher.TryStart(false, timespan.FromMilliseconds(1000)); resolver := CoCivicAddressResolver.CreateInstance; location := CoGeoCoordinate.Wrap(watcher.Position.Location); if location.IsUnknown = False then begin address := resolver.ResolveAddress(location); if not address.IsUnknown then begin Console.WriteLine_17('Country: {0}, Zip: {1}', address.CountryRegion, address.PostalCode); end end else begin Console.WriteLine_14('Address unknown.'); end; end; begin try Console := CoConsole.CreateInstance; ResolveAddressSync; except on E: Exception do begin Console.WriteLine_14(E.message); end; end; end.