remove LatLng coords wrapping/clamping, add explicit LatLng wrap method

This commit is contained in:
Vladimir Agafonkin 2012-12-11 15:02:47 +02:00
parent b9d0d60a04
commit f5cfab73a1
7 changed files with 29 additions and 20 deletions

View File

@ -2,7 +2,7 @@
L.LatLng represents a geographical point with latitude and longitude coordinates.
*/
L.LatLng = function (rawLat, rawLng, noWrap) { // (Number, Number[, Boolean])
L.LatLng = function (rawLat, rawLng) { // (Number, Number)
var lat = parseFloat(rawLat),
lng = parseFloat(rawLng);
@ -10,11 +10,6 @@ L.LatLng = function (rawLat, rawLng, noWrap) { // (Number, Number[, Boolean])
throw new Error('Invalid LatLng object: (' + rawLat + ', ' + rawLng + ')');
}
if (noWrap !== true) {
lat = Math.max(Math.min(lat, 90), -90); // clamp latitude into -90..90
lng = (lng + 180) % 360 + ((lng < -180 || lng === 180) ? 180 : -180); // wrap longitude into -180..180
}
this.lat = lat;
this.lng = lng;
};
@ -31,17 +26,21 @@ L.LatLng.prototype = {
obj = L.latLng(obj);
var margin = Math.max(Math.abs(this.lat - obj.lat), Math.abs(this.lng - obj.lng));
var margin = Math.max(
Math.abs(this.lat - obj.lat),
Math.abs(this.lng - obj.lng));
return margin <= L.LatLng.MAX_MARGIN;
},
toString: function (precision) { // -> String
toString: function (precision) { // (Number) -> String
return 'LatLng(' +
L.Util.formatNum(this.lat, precision) + ', ' +
L.Util.formatNum(this.lng, precision) + ')';
},
// Haversine distance formula, see http://en.wikipedia.org/wiki/Haversine_formula
// TODO move to projection code, LatLng shouldn't know about Earth
distanceTo: function (other) { // (LatLng) -> Number
other = L.latLng(other);
@ -57,10 +56,21 @@ L.LatLng.prototype = {
var a = sin1 * sin1 + sin2 * sin2 * Math.cos(lat1) * Math.cos(lat2);
return R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
},
wrap: function (a, b) { // (Number, Number) -> LatLng
var lng = this.lng;
a = a || -180;
b = b || 180;
lng = (lng + b) % (b - a) + (lng < a || lng === b ? b : a);
return new L.LatLng(this.lat, lng);
}
};
L.latLng = function (a, b, c) { // (LatLng) or ([Number, Number]) or (Number, Number, Boolean)
L.latLng = function (a, b) { // (LatLng) or ([Number, Number]) or (Number, Number)
if (a instanceof L.LatLng) {
return a;
}
@ -70,6 +80,6 @@ L.latLng = function (a, b, c) { // (LatLng) or ([Number, Number]) or (Number, Nu
if (isNaN(a)) {
return a;
}
return new L.LatLng(a, b, c);
return new L.LatLng(a, b);
};

View File

@ -23,8 +23,8 @@ L.LatLngBounds = L.Class.extend({
if (obj instanceof L.LatLng) {
if (!this._southWest && !this._northEast) {
this._southWest = new L.LatLng(obj.lat, obj.lng, true);
this._northEast = new L.LatLng(obj.lat, obj.lng, true);
this._southWest = new L.LatLng(obj.lat, obj.lng);
this._northEast = new L.LatLng(obj.lat, obj.lng);
} else {
this._southWest.lat = Math.min(obj.lat, this._southWest.lat);
this._southWest.lng = Math.min(obj.lng, this._southWest.lng);
@ -66,11 +66,11 @@ L.LatLngBounds = L.Class.extend({
},
getNorthWest: function () {
return new L.LatLng(this._northEast.lat, this._southWest.lng, true);
return new L.LatLng(this._northEast.lat, this._southWest.lng);
},
getSouthEast: function () {
return new L.LatLng(this._southWest.lat, this._northEast.lng, true);
return new L.LatLng(this._southWest.lat, this._northEast.lng);
},
contains: function (obj) { // (LatLngBounds) or (LatLng) -> Boolean

View File

@ -5,6 +5,6 @@ L.Projection.LonLat = {
},
unproject: function (point) {
return new L.LatLng(point.y, point.x, true);
return new L.LatLng(point.y, point.x);
}
};

View File

@ -47,6 +47,6 @@ L.Projection.Mercator = {
phi += dphi;
}
return new L.LatLng(phi * d, lng, true);
return new L.LatLng(phi * d, lng);
}
};

View File

@ -19,7 +19,6 @@ L.Projection.SphericalMercator = {
lng = point.x * d,
lat = (2 * Math.atan(Math.exp(point.y)) - (Math.PI / 2)) * d;
// TODO refactor LatLng wrapping
return new L.LatLng(lat, lng, true);
return new L.LatLng(lat, lng);
}
};

View File

@ -111,7 +111,7 @@ L.extend(L.GeoJSON, {
var lat = parseFloat(coords[reverse ? 0 : 1]),
lng = parseFloat(coords[reverse ? 1 : 0]);
return new L.LatLng(lat, lng, true);
return new L.LatLng(lat, lng);
},
coordsToLatLngs: function (coords, levelsDeep, reverse) { // (Array, Number, Boolean) -> Array

View File

@ -26,7 +26,7 @@ L.Circle = L.Path.extend({
projectLatlngs: function () {
var lngRadius = this._getLngRadius(),
latlng2 = new L.LatLng(this._latlng.lat, this._latlng.lng - lngRadius, true),
latlng2 = new L.LatLng(this._latlng.lat, this._latlng.lng - lngRadius),
point2 = this._map.latLngToLayerPoint(latlng2);
this._point = this._map.latLngToLayerPoint(this._latlng);