Class Location
Represents a position and possible velocity returned from positioning API's. This class is used both by foreground and background location for the purposes of both conveying the users location and conveying a desired location e.g. in the case of geofencing where we can define a location that would trigger the callback.
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 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());
The sample below demonstrates the usage of the background geofencing API:
// 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);
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCreates a comparator for sorting locations in order of increasing distance from the current location.floatReturns the horizontal accuracy of the location in metersdoubleReturns the altitude of this LocationfloatReturns the direction of this Location in degress 0-360doubleGets the distance in metres from the current location to another location.doubleReturns the latitude of this LocationdoubleReturns the longitude of this LocationdoubleReturns the longitude of this LocationintThe status of the location one of: LocationManager.AVAILABLE, LocationManager.OUT_OF_SERVICE or LocationManager.TEMPORARILY_UNAVAILABLE:longReturns the timestamp of this LocationfloatReturns the velocity of this Location in meters per second (m/s)voidsetAccuracy(float accuracy) voidsetAltitude(double altitude) Sets the altitude of this LocationvoidsetDirection(float direction) Sets the direction of this LocationvoidsetLatitude(double latitude) Sets the latitude of this LocationvoidsetLongitude(double longtitude) Sets the longitude of this LocationvoidsetLongtitude(double longtitude) Sets the longitude of this LocationvoidsetStatus(int status) The status of the location one of: LocationProvider.AVAILABLE, LocationProvider.OUT_OF_SERVICE or LocationProvider.TEMPORARILY_UNAVAILABLE:voidsetTimeStamp(long timeStamp) Sets the timeStamp of this LocationvoidsetVelocity(float velocity) Sets the velocity of this LocationtoString()
-
Constructor Details
-
Location
public Location() -
Location
public Location(double latitude, double longitude, double altitude, float direction) -
Location
public Location(double latitude, double longitude)
-
-
Method Details
-
getAccuracy
public float getAccuracy()Returns the horizontal accuracy of the location in meters
Returns
the accuracy if exists or 0.0 if not.
-
setAccuracy
public void setAccuracy(float accuracy) -
getAltitude
public double getAltitude()Returns the altitude of this Location
Returns
altitude
-
setAltitude
public void setAltitude(double altitude) Sets the altitude of this Location
Parameters
altitude
-
getDirection
public float getDirection()Returns the direction of this Location in degress 0-360
Returns
direction in degrees
-
setDirection
public void setDirection(float direction) Sets the direction of this Location
Parameters
direction
-
getLatitude
public double getLatitude()Returns the latitude of this Location
Returns
latitude
-
setLatitude
public void setLatitude(double latitude) Sets the latitude of this Location
Parameters
latitude
-
getLongitude
public double getLongitude()Returns the longitude of this Location
Returns
longitude
-
setLongitude
public void setLongitude(double longtitude) Sets the longitude of this Location
Parameters
longitude
-
getLongtitude
public double getLongtitude()Returns the longitude of this Location
Returns
longitude
Deprecated
use getLongitude
-
setLongtitude
public void setLongtitude(double longtitude) Sets the longitude of this Location
Parameters
longitude
Deprecated
use setLongitude
-
getStatus
public int getStatus()The status of the location one of: LocationManager.AVAILABLE, LocationManager.OUT_OF_SERVICE or LocationManager.TEMPORARILY_UNAVAILABLE: -
setStatus
public void setStatus(int status) The status of the location one of: LocationProvider.AVAILABLE, LocationProvider.OUT_OF_SERVICE or LocationProvider.TEMPORARILY_UNAVAILABLE: -
getTimeStamp
public long getTimeStamp()Returns the timestamp of this Location
Returns
timestamp
-
setTimeStamp
public void setTimeStamp(long timeStamp) Sets the timeStamp of this Location
Parameters
timeStamp
-
getVelocity
public float getVelocity()Returns the velocity of this Location in meters per second (m/s)
Returns
velocity
-
setVelocity
public void setVelocity(float velocity) Sets the velocity of this Location
Parameters
velocity
-
getDistanceTo
Gets the distance in metres from the current location to another location.
Parameters
l2: The location to measure distance to.
Returns
The number of metres between the current location and l2.
-
toString
-
createDistanceCompartor
Creates a comparator for sorting locations in order of increasing distance from the current location.
-