Class Location

java.lang.Object
com.codename1.location.Location

public class Location extends Object

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
    Constructor
    Description
     
    Location(double latitude, double longitude)
     
    Location(double latitude, double longitude, double altitude, float direction)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Creates a comparator for sorting locations in order of increasing distance from the current location.
    float
    Returns the horizontal accuracy of the location in meters
    double
    Returns the altitude of this Location
    float
    Returns the direction of this Location in degress 0-360
    double
    Gets the distance in metres from the current location to another location.
    double
    Returns the latitude of this Location
    double
    Returns the longitude of this Location
    double
    Returns the longitude of this Location
    int
    The status of the location one of: LocationManager.AVAILABLE, LocationManager.OUT_OF_SERVICE or LocationManager.TEMPORARILY_UNAVAILABLE:
    long
    Returns the timestamp of this Location
    float
    Returns the velocity of this Location in meters per second (m/s)
    void
    setAccuracy(float accuracy)
     
    void
    setAltitude(double altitude)
    Sets the altitude of this Location
    void
    setDirection(float direction)
    Sets the direction of this Location
    void
    setLatitude(double latitude)
    Sets the latitude of this Location
    void
    setLongitude(double longtitude)
    Sets the longitude of this Location
    void
    setLongtitude(double longtitude)
    Sets the longitude of this Location
    void
    setStatus(int status)
    The status of the location one of: LocationProvider.AVAILABLE, LocationProvider.OUT_OF_SERVICE or LocationProvider.TEMPORARILY_UNAVAILABLE:
    void
    setTimeStamp(long timeStamp)
    Sets the timeStamp of this Location
    void
    setVelocity(float velocity)
    Sets the velocity of this Location

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • 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

      public double getDistanceTo(Location l2)

      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

      public String toString()
      Overrides:
      toString in class Object
    • createDistanceCompartor

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