# Class Geofence

java.lang.Object
com.codename1.location.Geofence

public class Geofence extends Object

Metadata for geofencing support that allows tracking user location in the background while the app is inactive.

The sample below tracks location and posts a notification or shows a dialog based on the state of the app:

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

NOTE: For iOS you must include the `ios.background_modes` build hint with a value that includes "location" for geofencing to work.

Geofencing is not supported on all platforms, use `LocationManager#isGeofenceSupported()` to find out if the current platform supports it at runtime.

The maximum number of simulataneous Geofences allowed will vary by platform. iOS currently has a maximum of 20, and Android has a maximum of 100. If you need to track more than 20 at a time, consider using the `GeofenceManager` class to manage your Geofences, as it will allow you to effectively track an unlimited number of regions.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `Geofence(String id, Location loc, int radius, long expiration)`
  Constructor

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static Comparator<Geofence>`
  `createDistanceComparator(Geofence refRegion)`
  Creates a comparator for sorting Geofences from the current Geofence.
  `static Comparator<Geofence>`
  `createDistanceComparator(Location refPoint)`
  Creates a comparator for sorting Geofences from the given reference point.
  `boolean`
  `equals(Object o)`
  Geofences are equal if their id, radius, and expiration are the same, and the location latitude and longitude are the same.
  `double`
  `getDistanceTo(Geofence gf)`
  Gets the distance between the current region and the given region.
  `long`
  `getExpiration()`
  Gets the expiration duration (from now) of the Geofence in milliseconds.
  `String`
  `getId()`
  Gets the Geofence ID.
  `Location`
  `getLoc()`
  Gets the location of the Geofence.
  `int`
  `getRadius()`
  Gets the radius of the geofence in metres.
  `int`
  `hashCode()`
  Returns a hash code value for the object.

  ### Methods inherited from class Object

  `clone, getClass, notify, notifyAll, toString, wait, wait, wait`
  Modifier and Type
  Method
  Description
  `protected Object`
  `clone()`
  `final Class`
  `getClass()`
  Returns the runtime class of an object.
  `final void`
  `notify()`
  Wakes up a single thread that is waiting on this object's monitor.
  `final void`
  `notifyAll()`
  Wakes up all threads that are waiting on this object's monitor.
  `String`
  `toString()`
  Returns a string representation of the object.
  `final void`
  `wait()`
  Causes current thread to wait until another thread invokes the method or the method for this object.
  `final void`
  `wait(long timeout)`
  Causes current thread to wait until either another thread invokes the method or the method for this object, or a specified amount of time has elapsed.
  `final void`
  `wait(long timeout, int nanos)`
  Causes current thread to wait until another thread invokes the method or the method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.

- ## Constructor Details

  - ### Geofence

    public Geofence(String id, Location loc, int radius, long expiration)

    Constructor

    ###### Parameters

    - `id`: unique identifier

    - `loc`: the center location of this Geofence

    - `radius`: @param radius the radius in meters. Note that the actual radius will vary on an actual device depending on the hardware and OS. Typical android and iOS devices have a minimum radius of 100m.

    - `expiration`: the expiration time in milliseconds. Note that this is a duration, not a timestamp. Use -1 to never expire.

- ## Method Details

  - ### createDistanceComparator

    public static Comparator<Geofence> createDistanceComparator(Geofence refRegion)
    Creates a comparator for sorting Geofences from the current Geofence.

  - ### createDistanceComparator

    public static Comparator<Geofence> createDistanceComparator(Location refPoint)
    Creates a comparator for sorting Geofences from the given reference point.

  - ### getId

    public String getId()

    Gets the Geofence ID.

    ###### Returns

    the id

  - ### getLoc

    public Location getLoc()

    Gets the location of the Geofence.

    ###### Returns

    the center Location

  - ### getExpiration

    public long getExpiration()

    Gets the expiration duration (from now) of the Geofence in milliseconds.

    ###### Returns

    the Geofence expiration

  - ### getRadius

    public int getRadius()

    Gets the radius of the geofence in metres. Note that the actual radius will vary on an actual device depending on the hardware and OS. Typical android and iOS devices have a minimum radius of 100m.

    ###### Returns

    Geofence radius

  - ### getDistanceTo

    public double getDistanceTo(Geofence gf)

    Gets the distance between the current region and the given region.

    ###### Parameters

    - `gf`

  - ### equals

    public boolean equals(Object o)

    Geofences are equal if their id, radius, and expiration are the same, and the location latitude and longitude are the same.

    ###### Parameters

    - `o`
    Overrides:
      `equals` in class `Object`

  - ### hashCode

    public int hashCode()
    Description copied from class: `Object`
    Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable. The general contract of hashCode is: Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables. As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
    Overrides:
      `hashCode` in class `Object`