Get access to over 100 FireMonkey cross platform samples for Android, IOS, OSX, Windows, and Linux!

AndroidAppmethodCode SnippetDelphiDemoFiremonkeyIOS

Directly Access The GPS Location Sensor With The JNI In Delphi XE7 Firemonkey On Android

| Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey Delphi Android Delphi IOS

Delphi XE7 Firemonkey Android JNI GPS Location Sensor | Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey Delphi Android Delphi IOSDeveloper Jeff Overcash has a demo up on the Embarcadero Code Central which accesses the Android GPS functionality via the JNI in Delphi XE7 (and XE6) Firemonkey. If TLocationSensor does not quite work like you want you can go directly to the Android GPS data via the LocationManagerService. One goal of wanting to do this is you may be able to reduce the battery usage of accessing the GPS because your overhead may be less than the TLocationSensor component. The demo will give you longitude, latitude, and altitude. Additionally it will list the different GPS services available (on my device they were passive, GPS, and network). This demo is free and originally was for XE6 but I was able to update it to XE7 by simply adding Androidapi.Helpers to the uses clause. SharedActivityContext is now located in Androidapi.Helpers in Delphi XE7 Firemonkey. A tip for Firemonkey on IOS is that you can get location information directly from MKMapView using one of the DPF IOS or TMS Software wrapper components. This demo project and the IOS components should also work for Appmethod. Here is the main code from the demo:
procedure TForm1.Button1Click(Sender: TObject);
var
LocationManagerService: JObject;
iter : JIterator;
location : JLocation;
begin
if not Assigned(FLocationManager) then
begin
LocationManagerService := SharedActivityContext.getSystemService(TJContext.JavaClass.LOCATION_SERVICE);
FLocationManager := TJLocationManager.Wrap((LocationManagerService as ILocalObject).GetObjectID);
if not Assigned(locationListener) then
locationListener := TLocationListener.Create(self);
FLocationManager.requestLocationUpdates(TJLocationManager.JavaClass.GPS_PROVIDER, 10000, 10, locationListener,
TJLooper.JavaClass.getMainLooper);
end;
iter := FLocationManager.GetAllProviders.Iterator;
ListBox1.Clear;
while iter.hasNext do
begin
ListBox1.Items.Add(JStringToString(iter.next.ToString));
end;
CheckBox1.IsChecked := FLocationManager.isProviderEnabled(TJLocationManager.JavaClass.GPS_PROVIDER);
CheckBox2.IsChecked := FLocationManager.isProviderEnabled(TJLocationManager.JavaClass.NETWORK_PROVIDER);
location := FLocationManager.getLastKnownLocation(TJLocationManager.JavaClass.GPS_PROVIDER);
onLocationChanged(location);
end;

Download our mirror of the Android GPS JNI project demo with Delphi XE7 Firemonkey support.

Or head over and get the original Android GPS demo source code for Delphi XE6 from the Embarcadero Code Central.

Update: For Delphi 10.2 Tokyo you should check out the code for doing this on StackOverFlow.

Have Delphi Firemonkey questions? Ask and get answers on StackOverflow.

Related posts
DelphiDemoFiremonkeyLinuxOSXShowcaseWindows

AutoBlogAI: FireMonkey Client To Leverage LLMs And Generative AI For Blogging

DelphiFiremonkeyShowcaseUtilityWindows

Unleashing Creativity With Song Writer AI: A Deep Dive

DelphiFiremonkeyShowcaseWindows

How To Build Stable Diffusion Text To Image Prompts

AndroidC++BuilderDelphiFiremonkeyIOSOSXWindows

FireMonkey 10.4.2 Features Updated iOS 14, Android 11, And macOS 11 Support Plus Hundreds Of Fixes

Sign up for our Newsletter and
stay informed

3 Comments

Leave a Reply