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);
Types
class Geofence | Metadata for geofencing support that allows tracking user location in the background while the app is inactive. |
interface GeofenceListener | Listener interface for the Geofence background tracking functionality. |
class GeofenceManager | A utility class to simplify Geofencing in Codename One. |
class Location | Represents a position and possible velocity returned from positioning API’s. |
class LocationButton | A button that asks for the device location once, for a single transaction. |
interface LocationListener | This is a Listener to the Locations events see LocationManager.setLocationListener |
class LocationManager | The LocationManager is the main entry to retrieveLocation or to bind a LocationListener, important: in order to use location on iOS you will need to define the build argument ios.locationUsageDescription. |
class LocationRequest | This class is used when requesting to listen to location update. |
interface LocationSharedListener | Receives the location a LocationButton obtained. |