Package com.codename1.location
Abstraction of location services (GPS/Geofencing etc.) providing user global positioning and monitoring over such changes both in the foreground and background.
Trivial one time usage of location data can look like this sample:
Location position = LocationManager.getLocationManager().getCurrentLocationSync();
You can also track location in the foreground using API calls like this:
public class MyListener implements LocationListener {
public void locationUpdated(Location location) {
// update UI etc.
}
public void providerStateChanged(int newState) {
// handle status changes/errors appropriately
}
}
LocationManager.getLocationManager().setLocationListener(new MyListener());
Geofencing allows tracking whether a user entered a specific region, this can work when the app is completely in the background and is very efficient in terms of battery life:
// File: GeofenceListenerImpl.java
public class GeofenceListenerImpl implements GeofenceListener {
@Override
public void onExit(String id) {
}
@Override
public void onEntered(String id) {
if(!Display.getInstance().isMinimized()) {
Display.getInstance().callSerially(() -> {
Dialog.show("Welcome", "Thanks for arriving", "OK", null);
});
} else {
LocalNotification ln = new LocalNotification();
ln.setId("LnMessage");
ln.setAlertTitle("Welcome");
ln.setAlertBody("Thanks for arriving!");
Display.getInstance().scheduleLocalNotification(ln, System.currentTimeMillis() + 10, LocalNotification.REPEAT_NONE);
}
}
}
// File: GeofenceSample.java
Geofence gf = new Geofence("test", loc, 100, 100000);
LocationManager.getLocationManager().addGeoFencing(GeofenceListenerImpl.class, gf);
Everything above holds a precise-location grant for as long as the app is installed, which is
what navigation, tracking and geofencing need. A one-time use -- "what is near me", an address
fill, a single share -- wants LocationButton instead: on platforms that draw a location button
of their own, a tap on it grants precise location for that session and no longer, and Google Play
requires that route for transactional use in apps targeting Android 17 and later.
LocationButton share = new LocationButton(LocationButton.TEXT_SHARE_PRECISE_LOCATION);
share.addLocationSharedListener(loc -> {
if (loc != null) {
status.setText(loc.getLatitude() + ", " + loc.getLongitude());
status.getParent().revalidate();
}
});
-
ClassDescriptionMetadata for geofencing support that allows tracking user location in the background while the app is inactive.Listener interface for the Geofence background tracking functionality.A utility class to simplify Geofencing in Codename One.The Listener class that is registered to receive Geofence events.Represents a position and possible velocity returned from positioning API's.A button the user taps to share their precise location once.This is a Listener to the Locations events see LocationManager.setLocationListenerThe LocationManager is the main entry to retrieveLocation or to bind a LocationListener,This class is used when requesting to listen to location update.Receives the location a
LocationButtonobtained.