leaflet
Version:
JavaScript library for mobile-friendly interactive maps
87 lines (70 loc) • 2.07 kB
JavaScript
/*
* L.LatLng represents a geographical point with latitude and longitude coordinates.
*/
L.LatLng = function (lat, lng, alt) {
if (isNaN(lat) || isNaN(lng)) {
throw new Error('Invalid LatLng object: (' + lat + ', ' + lng + ')');
}
this.lat = +lat;
this.lng = +lng;
if (alt !== undefined) {
this.alt = +alt;
}
};
L.LatLng.prototype = {
equals: function (obj, maxMargin) {
if (!obj) { return false; }
obj = L.latLng(obj);
var margin = Math.max(
Math.abs(this.lat - obj.lat),
Math.abs(this.lng - obj.lng));
return margin <= (maxMargin === undefined ? 1.0E-9 : maxMargin);
},
toString: function (precision) {
return 'LatLng(' +
L.Util.formatNum(this.lat, precision) + ', ' +
L.Util.formatNum(this.lng, precision) + ')';
},
distanceTo: function (other) {
return L.CRS.Earth.distance(this, L.latLng(other));
},
wrap: function () {
return L.CRS.Earth.wrapLatLng(this);
},
toBounds: function (sizeInMeters) {
var latAccuracy = 180 * sizeInMeters / 40075017,
lngAccuracy = latAccuracy / Math.cos((Math.PI / 180) * this.lat);
return L.latLngBounds(
[this.lat - latAccuracy, this.lng - lngAccuracy],
[this.lat + latAccuracy, this.lng + lngAccuracy]);
},
clone: function () {
return new L.LatLng(this.lat, this.lng, this.alt);
}
};
// constructs LatLng with different signatures
// (LatLng) or ([Number, Number]) or (Number, Number) or (Object)
L.latLng = function (a, b, c) {
if (a instanceof L.LatLng) {
return a;
}
if (L.Util.isArray(a) && typeof a[0] !== 'object') {
if (a.length === 3) {
return new L.LatLng(a[0], a[1], a[2]);
}
if (a.length === 2) {
return new L.LatLng(a[0], a[1]);
}
return null;
}
if (a === undefined || a === null) {
return a;
}
if (typeof a === 'object' && 'lat' in a) {
return new L.LatLng(a.lat, 'lng' in a ? a.lng : a.lon, a.alt);
}
if (b === undefined) {
return null;
}
return new L.LatLng(a, b, c);
};