1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-29 07:54:19 +00:00
pds-2021-g2-nest/nest_frontend/objects/MapArea.js

65 lines
1.6 KiB
JavaScript
Raw Normal View History

import {getDistance} from "geolib"
2021-05-22 02:08:39 +00:00
import osmZoomLevels from "../utils/osmZoomLevels"
/**
* An area on a map, defined by a `center` and a `radius` in meters.
*/
export default class MapArea {
radius
center
/**
* @param radius - The radius of the area in meters.
* @param center - The center of the area.
*/
constructor(radius, center) {
this.radius = radius
this.center = center
}
2021-05-22 02:08:39 +00:00
/**
* Create a new {@link MapArea} from the [zoom level of OpenStreetMaps][1], assuming the window is
* ~400 pixels large.
*
* [1]: https://wiki.openstreetmap.org/wiki/Zoom_levels
*
* @param zoom
* @param center
* @returns {MapArea}
*/
static fromZoomLevel(zoom, center) {
return new MapArea(osmZoomLevels[zoom], center)
}
/**
* @returns {string}
*/
toString() {
2021-05-22 02:32:47 +00:00
return `< ${this.radius} ${this.center.toString()}`
}
/**
2021-05-22 02:08:39 +00:00
* Render the {@link MapArea} as an human-readable string.
*
* @returns {string}
*/
toHumanString() {
if(this.radius >= 2000) {
const kmRadius = Math.round(this.radius / 1000)
2021-05-22 02:32:47 +00:00
return `< ${kmRadius}km ${this.center.toHumanString()}`
}
2021-05-22 02:32:47 +00:00
return `< ${this.radius}m ${this.center.toHumanString()}`
}
/**
* Check if a pair of coordinates is included in the area.
*
* @param coords - The coordinates to check.
* @returns {boolean}
*/
includes(coords) {
return getDistance(this.center.toGeolib(), coords.toGeolib()) <= this.radius
}
}