# Package com.codename1.location

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);

- Class
  Description
  Geofence
  Metadata for geofencing support that allows tracking user location in the background while the app is inactive.
  GeofenceListener
  Listener interface for the Geofence background tracking functionality.
  GeofenceManager
  A utility class to simplify Geofencing in Codename One.
  GeofenceManager.Listener
  The Listener class that is registered to receive Geofence events.
  Location
  Represents a position and possible velocity returned from positioning API's.
  LocationListener
  This is a Listener to the Locations events see LocationManager.setLocationListener
  LocationManager
  The LocationManager is the main entry to retrieveLocation or to bind a LocationListener,
  LocationRequest
  This class is used when requesting to listen to location update.