forked from 0x2620/oxjs
improvements to map
This commit is contained in:
parent
4326c307b4
commit
2e645418dd
9 changed files with 980 additions and 285 deletions
140
build/js/ox.js
140
build/js/ox.js
|
|
@ -415,6 +415,7 @@ Ox.isEqual = function(obj0, obj1) {
|
|||
if (obj0 == obj1) {
|
||||
ret = true;
|
||||
} else if (Ox.isArray(obj0) && obj0.length == obj1.length) {
|
||||
ret = true;
|
||||
Ox.forEach(obj0, function(v, i) {
|
||||
ret = Ox.isEqual(v, obj1[i]);
|
||||
return ret;
|
||||
|
|
@ -1331,7 +1332,7 @@ Encoding functions
|
|||
>>> Ox.encodeUTF8('¥€$')
|
||||
'\u00C2\u00A5\u00E2\u0082\u00AC\u0024'
|
||||
*/
|
||||
return Ox.map(Array.prototype.slice.call(str), function(chr) {
|
||||
return Ox.map(str, function(chr) {
|
||||
var code = chr.charCodeAt(0),
|
||||
str = '';
|
||||
if (code < 128) {
|
||||
|
|
@ -1399,6 +1400,10 @@ Format functions
|
|||
================================================================================
|
||||
*/
|
||||
|
||||
Ox.formatArea = function(num, dec) {
|
||||
return Ox.formatNumber(Ox.round(num / 1000000, dec)) + ' km²';
|
||||
}
|
||||
|
||||
Ox.formatColor = function() {
|
||||
|
||||
};
|
||||
|
|
@ -1713,6 +1718,132 @@ Geo functions
|
|||
================================================================================
|
||||
*/
|
||||
|
||||
(function() {
|
||||
|
||||
function rad(point) {
|
||||
return {
|
||||
lat: Ox.rad(point.lat),
|
||||
lng: Ox.rad(point.lng)
|
||||
};
|
||||
}
|
||||
|
||||
Ox.crossesDateline = function(point0, point1) {
|
||||
return point0.lng > point1.lng;
|
||||
}
|
||||
|
||||
Ox.getArea = function(point0, point1) {
|
||||
/*
|
||||
area of a ring between two latitudes:
|
||||
2 * PI * r^2 * abs(sin(lat0) - sin(lat1))
|
||||
see http://mathforum.org/library/drmath/view/63767.html
|
||||
*/
|
||||
/*
|
||||
2 * Math.PI *
|
||||
Math.pow(Ox.EARTH_RADIUS, 2) *
|
||||
Math.abs(Math.sin(Ox.rad(0)) - Math.sin(Ox.rad(1))) *
|
||||
Math.abs(Ox.rad(0) - Ox.rad(1)) /
|
||||
(2 * Math.PI)
|
||||
*/
|
||||
if (Ox.crossesDateline(point0, point1)) {
|
||||
point1.lng += 360;
|
||||
}
|
||||
var point0 = rad(point0),
|
||||
point1 = rad(point1);
|
||||
return Math.pow(Ox.EARTH_RADIUS, 2) *
|
||||
Math.abs(Math.sin(point0.lat) - Math.sin(point1.lat)) *
|
||||
Math.abs(point0.lng - point1.lng);
|
||||
};
|
||||
|
||||
Ox.getBearing = function(point0, point1) {
|
||||
/*
|
||||
>>> Ox.getBearing({lat: -45, lng: 0}, {lat: 45, lng: 0})
|
||||
0
|
||||
*/
|
||||
var point0 = rad(point0),
|
||||
point1 = rad(point1),
|
||||
x = Math.cos(point0.lat) * Math.sin(point1.lat) -
|
||||
Math.sin(point0.lat) * Math.cos(point1.lat) *
|
||||
Math.cos(point1.lng - point0.lng),
|
||||
y = Math.sin(point1.lng - point0.lng) *
|
||||
Math.cos(point1.lat);
|
||||
return (Ox.deg(Math.atan2(y, x)) + 360) % 360;
|
||||
};
|
||||
|
||||
Ox.getCenter = function(point0, point1) {
|
||||
/*
|
||||
>>> Ox.values(Ox.getCenter({lat: -45, lng: -90}, {lat: 45, lng: 90}))
|
||||
[0, 0]
|
||||
*/
|
||||
var point0 = rad(point0),
|
||||
point1 = rad(point1),
|
||||
x = Math.cos(point1.lat) *
|
||||
Math.cos(point1.lng - point0.lng),
|
||||
y = Math.cos(point1.lat) *
|
||||
Math.sin(point1.lng - point0.lng),
|
||||
d = Math.sqrt(
|
||||
Math.pow(Math.cos(point0.lat) + x, 2) + Math.pow(y, 2)
|
||||
),
|
||||
lat = Ox.deg(
|
||||
Math.atan2(Math.sin(point0.lat) + Math.sin(point1.lat), d)
|
||||
),
|
||||
lng = Ox.deg(
|
||||
point0.lng + Math.atan2(y, Math.cos(point0.lat) + x)
|
||||
);
|
||||
return {lat: lat, lng: lng};
|
||||
};
|
||||
|
||||
Ox.getDistance = function(point0, point1) {
|
||||
/*
|
||||
>>> Ox.EARTH_CIRCUMFERENCE == Ox.getDistance({lat: -45, lng: -90}, {lat: 45, lng: 90}) * 2
|
||||
true
|
||||
*/
|
||||
var point0 = rad(point0),
|
||||
point1 = rad(point1);
|
||||
return Math.acos(
|
||||
Math.sin(point0.lat) * Math.sin(point1.lat) +
|
||||
Math.cos(point0.lat) * Math.cos(point1.lat) *
|
||||
Math.cos(point1.lng - point0.lng)
|
||||
) * Ox.EARTH_RADIUS;
|
||||
};
|
||||
|
||||
Ox.getLatLngByXY = function(xy) {
|
||||
/*
|
||||
>>> Ox.values(Ox.getLatLngByXY({x: 0.5, y: 0.5}))
|
||||
[0, 0]
|
||||
*/
|
||||
function getVal(val) {
|
||||
return (val - 0.5) * 2 * Math.PI;
|
||||
}
|
||||
return {
|
||||
lat: -Ox.deg(Math.atan(Ox.sinh(getVal(xy.y)))),
|
||||
lng: Ox.deg(getVal(xy.x))
|
||||
}
|
||||
};
|
||||
|
||||
Ox.getMetersPerDegree = function(lat) {
|
||||
/*
|
||||
>>> Ox.EARTH_CIRCUMFERENCE == Ox.getMetersPerDegree(0) * 360
|
||||
true
|
||||
*/
|
||||
return Math.cos(lat * Math.PI / 180) * Ox.EARTH_CIRCUMFERENCE / 360;
|
||||
};
|
||||
|
||||
Ox.getXYByLatLng = function(latlng) {
|
||||
/*
|
||||
>>> Ox.values(Ox.getXYByLatLng({lat: 0, lng: 0}))
|
||||
[0.5, 0.5]
|
||||
*/
|
||||
function getVal(val) {
|
||||
return (val / (2 * Math.PI) + 0.5)
|
||||
}
|
||||
return {
|
||||
x: getVal(Ox.rad(latlng.lng)),
|
||||
y: getVal(Ox.asinh(Math.tan(Ox.rad(-latlng.lat))))
|
||||
};
|
||||
};
|
||||
|
||||
}());
|
||||
|
||||
Ox.Line = function(point0, point1) {
|
||||
|
||||
var self = {
|
||||
|
|
@ -1734,13 +1865,6 @@ Ox.Line = function(point0, point1) {
|
|||
};
|
||||
|
||||
that.getBearing = function() {
|
||||
var points = rad(),
|
||||
x = Math.cos(point[0].lat) * Math.sin(point[1].lat) -
|
||||
Math.sin(point[0].lat) * Math.cos(point[1].lat) *
|
||||
Math.cos(point[1].lng - point[0].lng),
|
||||
y = Math.sin(point[1].lng - point[0].lng) *
|
||||
Math.cos(point[1].lat);
|
||||
return (Ox.deg(math.atan2(y, x)) + 360) % 360;
|
||||
};
|
||||
|
||||
that.getDistance = function() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue