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:
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 and Description |
---|
Geofence(String id,
Location loc,
int radius,
long expiration)
Constructor
|
Modifier and Type | Method and 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.
|
public Geofence(String id, Location loc, int radius, long expiration)
id
- unique identifierloc
- the center location of this Geofenceradius
- 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.public String getId()
public Location getLoc()
public long getExpiration()
public int getRadius()
public double getDistanceTo(Geofence gf)
gf
- public static Comparator<Geofence> createDistanceComparator(Geofence refRegion)
public static Comparator<Geofence> createDistanceComparator(Location refPoint)