maplair
Version:
JavaScript library for mobile-friendly interactive maps
114 lines (113 loc) • 3.63 kB
JavaScript
import { LatLng } from "./LatLng.js";
//#region src/geo/LatLngBounds.js
var LatLngBounds = class LatLngBounds {
constructor(corner1, corner2) {
if (!corner1) return;
if (corner1 instanceof LatLngBounds) {
if (corner1.isValid()) {
this._southWest = new LatLng(corner1._southWest);
this._northEast = new LatLng(corner1._northEast);
}
return;
}
const latlngs = corner2 ? [corner1, corner2] : corner1;
for (const latlng of latlngs) this.extend(latlng);
}
extend(obj) {
const sw = this._southWest, ne = this._northEast;
let sw2, ne2;
if (obj instanceof LatLng) {
sw2 = obj;
ne2 = obj;
} else if (obj instanceof LatLngBounds) {
sw2 = obj._southWest;
ne2 = obj._northEast;
if (!sw2 || !ne2) return this;
} else {
if (!obj) return this;
if (LatLng.validate(obj)) return this.extend(new LatLng(obj));
return this.extend(new LatLngBounds(obj));
}
if (!sw && !ne) {
this._southWest = new LatLng(sw2.lat, sw2.lng);
this._northEast = new LatLng(ne2.lat, ne2.lng);
} else {
sw.lat = Math.min(sw2.lat, sw.lat);
sw.lng = Math.min(sw2.lng, sw.lng);
ne.lat = Math.max(ne2.lat, ne.lat);
ne.lng = Math.max(ne2.lng, ne.lng);
}
return this;
}
pad(bufferRatio) {
const sw = this._southWest, ne = this._northEast, heightBuffer = Math.abs(sw.lat - ne.lat) * bufferRatio, widthBuffer = Math.abs(sw.lng - ne.lng) * bufferRatio;
return new LatLngBounds(new LatLng(sw.lat - heightBuffer, sw.lng - widthBuffer), new LatLng(ne.lat + heightBuffer, ne.lng + widthBuffer));
}
getCenter() {
return new LatLng((this._southWest.lat + this._northEast.lat) / 2, (this._southWest.lng + this._northEast.lng) / 2);
}
getSouthWest() {
return this._southWest;
}
getNorthEast() {
return this._northEast;
}
getNorthWest() {
return new LatLng(this.getNorth(), this.getWest());
}
getSouthEast() {
return new LatLng(this.getSouth(), this.getEast());
}
getWest() {
return this._southWest.lng;
}
getSouth() {
return this._southWest.lat;
}
getEast() {
return this._northEast.lng;
}
getNorth() {
return this._northEast.lat;
}
contains(obj) {
if (LatLng.validate(obj)) obj = new LatLng(obj);
else obj = new LatLngBounds(obj);
const sw = this._southWest, ne = this._northEast;
let sw2, ne2;
if (obj instanceof LatLngBounds) {
sw2 = obj.getSouthWest();
ne2 = obj.getNorthEast();
} else sw2 = ne2 = obj;
return sw2.lat >= sw.lat && ne2.lat <= ne.lat && sw2.lng >= sw.lng && ne2.lng <= ne.lng;
}
intersects(bounds) {
bounds = new LatLngBounds(bounds);
const sw = this._southWest, ne = this._northEast, sw2 = bounds.getSouthWest(), ne2 = bounds.getNorthEast(), latIntersects = ne2.lat >= sw.lat && sw2.lat <= ne.lat, lngIntersects = ne2.lng >= sw.lng && sw2.lng <= ne.lng;
return latIntersects && lngIntersects;
}
overlaps(bounds) {
bounds = new LatLngBounds(bounds);
const sw = this._southWest, ne = this._northEast, sw2 = bounds.getSouthWest(), ne2 = bounds.getNorthEast(), latOverlaps = ne2.lat > sw.lat && sw2.lat < ne.lat, lngOverlaps = ne2.lng > sw.lng && sw2.lng < ne.lng;
return latOverlaps && lngOverlaps;
}
toBBoxString() {
return [
this.getWest(),
this.getSouth(),
this.getEast(),
this.getNorth()
].join(",");
}
equals(bounds, maxMargin) {
if (!bounds) return false;
bounds = new LatLngBounds(bounds);
return this._southWest.equals(bounds.getSouthWest(), maxMargin) && this._northEast.equals(bounds.getNorthEast(), maxMargin);
}
isValid() {
return !!(this._southWest && this._northEast);
}
};
//#endregion
export { LatLngBounds };
//# sourceMappingURL=LatLngBounds.js.map