public class Location

  1. Object
  2. 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 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());

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

Constructors

public Location()
public Location(double latitude, double longitude, double altitude, float direction)
public Location(double latitude, double longitude)

Methods

public float getAccuracy()Returns the horizontal accuracy of the location in meters
public void setAccuracy(float accuracy)
public double getAltitude()Returns the altitude of this Location
public void setAltitude(double altitude)Sets the altitude of this Location
public float getDirection()Returns the direction of this Location in degress 0-360
public void setDirection(float direction)Sets the direction of this Location
public double getLatitude()Returns the latitude of this Location
public void setLatitude(double latitude)Sets the latitude of this Location
public double getLongitude()Returns the longitude of this Location
public void setLongitude(double longtitude)Sets the longitude of this Location
public double getLongtitude()Deprecated Returns the longitude of this Location
public void setLongtitude(double longtitude)Deprecated Sets the longitude of this Location
public int getStatus()The status of the location one of: LocationManager.AVAILABLE, LocationManager.OUT_OF_SERVICE or LocationManager.TEMPORARILY_UNAVAILABLE:
public void setStatus(int status)The status of the location one of: LocationProvider.AVAILABLE, LocationProvider.OUT_OF_SERVICE or LocationProvider.TEMPORARILY_UNAVAILABLE:
public long getTimeStamp()Returns the timestamp of this Location
public void setTimeStamp(long timeStamp)Sets the timeStamp of this Location
public float getVelocity()Returns the velocity of this Location in meters per second (m/s)
public void setVelocity(float velocity)Sets the velocity of this Location
public double getDistanceTo(Location l2)Gets the distance in metres from the current location to another location.
public String toString()Returns a string representation of the object.
public Comparator<Location> createDistanceCompartor()Creates a comparator for sorting locations in order of increasing distance from the current location.

Inherited methods

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

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

getLatitude

public double getLatitude()
Returns the latitude of this Location

Returns

latitude

setLatitude

public void setLatitude(double latitude)
Sets the latitude of this Location

getLongitude

public double getLongitude()
Returns the longitude of this Location

Returns

longitude

setLongitude

public void setLongitude(double longtitude)
Sets the longitude of this Location

getLongtitude

public double getLongtitude()
Deprecated. use getLongitude
Returns the longitude of this Location

Returns

longitude

setLongtitude

public void setLongtitude(double longtitude)
Deprecated. use setLongitude
Sets the longitude of this Location

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

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

getDistanceTo

public double getDistanceTo(Location l2)
Gets the distance in metres from the current location to another location.

Parameters

l2 Location
The location to measure distance to.

Returns

The number of metres between the current location and l2.

toString

public String toString()
Returns a string representation of the object. In general, the toString method returns a string that “textually represents” this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@’, and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + ‘@’ + Integer.toHexString(hashCode())

createDistanceCompartor

public Comparator<Location> createDistanceCompartor()
Creates a comparator for sorting locations in order of increasing distance from the current location.