geolocation-utils
Version:
A utility library for calculations with geolocations
38 lines (33 loc) • 1.15 kB
JavaScript
const geo = require('..')
// convert various location formats
console.log(geo.toLatLon([4, 51])) // { lat: 51, lon: 4 }
console.log(geo.toLatitudeLongitude({ lat: 51, lng: 4 })) // { latitude: 51, longitude: 4 }
// calculate the distance between locations, move to a new location
const location1 = {lat: 51, lon: 4}
const location2 = {lat: 51.001, lon: 4.001 }
console.log(geo.headingDistanceTo(location1, location2))
// {
// heading: 32.1..., // degrees
// distance: 131.5... // meters
// }
console.log(geo.moveTo(location1, {heading: 32.1, distance: 131.5}))
// {
// lat: 51.001...,
// lon: 4.001...,
// }
// check whether a location is inside a circle, bounding box, or polygon
const polygon = [
[],
[],
[],
[],
[],
[],
[],
[]
]
console.log(geo.insidePolygon([4.03324, 51.9632], polygon)) // true
// and much more:
// - calculate bounding boxes around a list of locations,
// - calculate the closest point of approach of two moving objects,
// - etc...