remove LatLng constants, add move maxMargin to equals arguments

This commit is contained in:
Vladimir Agafonkin 2013-12-03 19:09:36 +02:00
parent 3d7520ff27
commit b151ceb63f
5 changed files with 16 additions and 22 deletions

View File

@ -2,7 +2,7 @@
* L.LatLng represents a geographical point with latitude and longitude coordinates. * L.LatLng represents a geographical point with latitude and longitude coordinates.
*/ */
L.LatLng = function (lat, lng, alt) { // (Number, Number, Number) L.LatLng = function (lat, lng, alt) {
lat = parseFloat(lat); lat = parseFloat(lat);
lng = parseFloat(lng); lng = parseFloat(lng);
@ -18,14 +18,8 @@ L.LatLng = function (lat, lng, alt) { // (Number, Number, Number)
} }
}; };
L.extend(L.LatLng, {
DEG_TO_RAD: Math.PI / 180,
RAD_TO_DEG: 180 / Math.PI,
MAX_MARGIN: 1.0E-9 // max margin of error for the "equals" check
});
L.LatLng.prototype = { L.LatLng.prototype = {
equals: function (obj) { // (LatLng) -> Boolean equals: function (obj, maxMargin) {
if (!obj) { return false; } if (!obj) { return false; }
obj = L.latLng(obj); obj = L.latLng(obj);
@ -34,10 +28,10 @@ L.LatLng.prototype = {
Math.abs(this.lat - obj.lat), Math.abs(this.lat - obj.lat),
Math.abs(this.lng - obj.lng)); Math.abs(this.lng - obj.lng));
return margin <= L.LatLng.MAX_MARGIN; return margin <= (maxMargin === undefined ? 1.0E-9 : maxMargin);
}, },
toString: function (precision) { // (Number) -> String toString: function (precision) {
return 'LatLng(' + return 'LatLng(' +
L.Util.formatNum(this.lat, precision) + ', ' + L.Util.formatNum(this.lat, precision) + ', ' +
L.Util.formatNum(this.lng, precision) + ')'; L.Util.formatNum(this.lng, precision) + ')';
@ -45,15 +39,15 @@ L.LatLng.prototype = {
// Haversine distance formula, see http://en.wikipedia.org/wiki/Haversine_formula // Haversine distance formula, see http://en.wikipedia.org/wiki/Haversine_formula
// TODO move to projection code, LatLng shouldn't know about Earth // TODO move to projection code, LatLng shouldn't know about Earth
distanceTo: function (other) { // (LatLng) -> Number distanceTo: function (other) {
other = L.latLng(other); other = L.latLng(other);
var R = 6378137, // earth radius in meters var R = 6378137, // earth radius in meters
d2r = L.LatLng.DEG_TO_RAD, rad = Math.PI / 180,
dLat = (other.lat - this.lat) * d2r, dLat = (other.lat - this.lat) * rad,
dLon = (other.lng - this.lng) * d2r, dLon = (other.lng - this.lng) * rad,
lat1 = this.lat * d2r, lat1 = this.lat * rad,
lat2 = other.lat * d2r, lat2 = other.lat * rad,
sin1 = Math.sin(dLat / 2), sin1 = Math.sin(dLat / 2),
sin2 = Math.sin(dLon / 2); sin2 = Math.sin(dLon / 2);

View File

@ -12,7 +12,7 @@ L.Projection.Mercator = {
bounds: L.bounds([-20037508.34279, -15496570.73972], [20037508.34279, 18764656.23138]), bounds: L.bounds([-20037508.34279, -15496570.73972], [20037508.34279, 18764656.23138]),
project: function (latlng) { project: function (latlng) {
var d = L.LatLng.DEG_TO_RAD, var d = Math.PI / 180,
max = this.MAX_LATITUDE, max = this.MAX_LATITUDE,
lat = Math.max(Math.min(max, latlng.lat), -max), lat = Math.max(Math.min(max, latlng.lat), -max),
r = this.R_MAJOR, r = this.R_MAJOR,
@ -32,7 +32,7 @@ L.Projection.Mercator = {
}, },
unproject: function (point) { unproject: function (point) {
var d = L.LatLng.RAD_TO_DEG, var d = 180 / Math.PI,
r = this.R_MAJOR, r = this.R_MAJOR,
r2 = this.R_MINOR, r2 = this.R_MINOR,
lng = point.x * d / r, lng = point.x * d / r,

View File

@ -9,7 +9,7 @@ L.Projection.SphericalMercator = {
R: 6378137, R: 6378137,
project: function (latlng) { project: function (latlng) {
var d = L.LatLng.DEG_TO_RAD, var d = Math.PI / 180,
max = this.MAX_LATITUDE, max = this.MAX_LATITUDE,
x = this.R * latlng.lng * d, x = this.R * latlng.lng * d,
y = Math.max(Math.min(max, latlng.lat), -max) * d; y = Math.max(Math.min(max, latlng.lat), -max) * d;
@ -20,7 +20,7 @@ L.Projection.SphericalMercator = {
}, },
unproject: function (point) { unproject: function (point) {
var d = L.LatLng.RAD_TO_DEG, var d = 180 / Math.PI,
lng = point.x * d / this.R, lng = point.x * d / this.R,
lat = (2 * Math.atan(Math.exp(point.y / this.R)) - (Math.PI / 2)) * d; lat = (2 * Math.atan(Math.exp(point.y / this.R)) - (Math.PI / 2)) * d;

View File

@ -77,7 +77,7 @@ L.Circle = L.Path.extend({
}, },
_getLngRadius: function () { _getLngRadius: function () {
return this._getLatRadius() / Math.cos(L.LatLng.DEG_TO_RAD * this._latlng.lat); return this._getLatRadius() / Math.cos((Math.PI / 180) * this._latlng.lat);
}, },
_checkIfEmpty: function () { _checkIfEmpty: function () {

View File

@ -68,7 +68,7 @@ L.Map.include({
latlng = new L.LatLng(lat, lng), latlng = new L.LatLng(lat, lng),
latAccuracy = 180 * pos.coords.accuracy / 40075017, latAccuracy = 180 * pos.coords.accuracy / 40075017,
lngAccuracy = latAccuracy / Math.cos(L.LatLng.DEG_TO_RAD * lat), lngAccuracy = latAccuracy / Math.cos((Math.PI / 180) * lat),
bounds = L.latLngBounds( bounds = L.latLngBounds(
[lat - latAccuracy, lng - lngAccuracy], [lat - latAccuracy, lng - lngAccuracy],