Geolocation

Provides a consistent API to the geolocation on all platforms.

Provides geolocation data in terms of longitude and latitude coordinates on the Earth's surface and provides helper functions to measure distance and bearing between geolocations.

Example

// distance to the Adtile office from your current position
var adtile = new MotionStack.Geolocation.LatLon({
  lat: 32.914,
  lon: -117.235
});

var geolocation = new MotionStack.Geolocation.currentPostion(function(e) {
  var distance = adtile.distanceTo(e);
  console.log(distance);
});

Constructor

new MotionStack.Geolocation.LatLon(position)

Constructor to create a new instance of the LatLon class. An instance of this class is passed into the callback function through the start method.

Argument Type Description
position Object An object representing the latitude and longitude of the origin.

  • e.lat {Number}
    Earth-based latitude of the device (degrees)
  • e.lon {Number}
    Earth-based longitude of the device (degrees)
  • e.accuracy {Number}
    Accuracy of latitude and longitude (meters)
    Default: null
  • e.timeStamp {Number} (optional)
    The time in milliseconds when the data is collected
    Default: Date.now()
  • altitude {Number} (optional)
    Earth-based altitude of the device (meters)
    Default: null
  • altitudeAccuracy {Number} (optional)
    Accuracy of altitude measurement (meters)
    Default: null
  • speed {Number} (optional)
    Velocity of the device (m/s)
    Default: null
  • heading {Number} (optional)
    Angular measurement of device heading relative to north (degrees)
    Default: null
  • error {Error} (optional)
    Error in case the LatLon is constructed from an erroneous reading. This is used when the browser fails to return Geolocation data.
    Default: null

Instance Methods

distanceTo(point, [precision])

Returns the distance (in kilometers) from the user's current geolocation to the destination point. Returns null if the user's current geolocation hasn't been computed yet.

Argument Type Description
point Object An object representing the latitude and longitude of the destination point.

  • lat {Number}
  • Earth-based latitude of the destination point (degrees)
  • lon {Number}
  • Earth-based longitude of the destination point (degrees)
precision Number Number of significant digits to use for returned value.
Default: 4
// How far to the Googleplex from the Adtile office?
var adtile = new MotionStack.Geolocation.LatLon({
  lat: 32.914,
  lon: -117.235
});

var distance = adtile.distanceTo({ lat: 37.422, lon: -122.084 });
console.log(distance + "km");

bearingTo(point)

Returns the bearing (in degrees) from the user's current geolocation to the destination point. Returns null if the user's current geolocation hasn't been computed yet.

Argument Type Description
point Object An object representing the latitude and longitude of the destination point.

  • lat {Number}
  • Earth-based latitude of the destination point (degrees)
  • lon {Number}
  • Earth-based longitude of the destination point (degrees)
// Which direction is the Googleplex from the Adtile office?
var adtile = new MotionStack.Geolocation.LatLon({
  lat: 32.914,
  lon: -117.235
});

var bearing = adtile.bearingTo({ lat: 37.422, lon: -122.084 });
console.log(bearing + " degrees");

Source

src/helpers/gps.js