mirror of
https://github.com/kiwix/kiwix-js.git
synced 2025-09-23 04:28:30 -04:00
Add the cardinal direction to articles (nearby article search). Fixes #83
This commit is contained in:
parent
8bff5cd843
commit
5d354dbd71
@ -283,6 +283,21 @@ define(function(require) {
|
||||
ok(rect1.contains(rect6), "rect1 should contain rect6");
|
||||
ok(rect1.contains(rect7), "rect1 should contain rect7");
|
||||
});
|
||||
test("check bearing algorithm", function() {
|
||||
var point1 = new geometry.point(0,0);
|
||||
var point2 = new geometry.point(2,0);
|
||||
var line1 = new geometry.line(point1, point2);
|
||||
equal(line1.bearing(), "N", "Bearing of line1 should be N");
|
||||
var pointLondon = new geometry.point(51.50777816772461, -0.12805555760860443);
|
||||
var pointParis = new geometry.point(48.856614, 2.3522219000000177);
|
||||
var pointAmsterdam = new geometry.point(52.326947, 4.741287);
|
||||
var lineLondonParis = new geometry.line(pointLondon, pointParis);
|
||||
var lineParisLondon = new geometry.line(pointParis, pointLondon);
|
||||
var lineLondonAmsterdam = new geometry.line(pointLondon, pointAmsterdam);
|
||||
equal(lineLondonParis.bearing(), "SE", "Bearing from London to Paris sould be SE");
|
||||
equal(lineParisLondon.bearing(), "NW", "Bearing from Paris to London sould be NW");
|
||||
equal(lineLondonAmsterdam.bearing(), "E", "Bearing from London to Amsterdam sould be E");
|
||||
});
|
||||
|
||||
module("utils");
|
||||
test("check reading an IEEE_754 float from 4 bytes" ,function() {
|
||||
|
@ -416,9 +416,10 @@ define(function(require) {
|
||||
|
||||
var distanceFromHereHtml = "";
|
||||
if (title._geolocation && currentCoordinates) {
|
||||
// If we know the current position and the title position, we display the distance
|
||||
// If we know the current position and the title position, we display the distance and cardinal direction
|
||||
var distanceKm = (currentCoordinates.distance(title._geolocation) * 6371 / 60).toFixed(1);
|
||||
distanceFromHereHtml = " (" + distanceKm + " km)";
|
||||
var cardinalDirection = currentCoordinates.bearing(title._geolocation);
|
||||
distanceFromHereHtml = " (" + distanceKm + " km " + cardinalDirection + ")";
|
||||
}
|
||||
|
||||
titleListDivHtml += "<a href='#' titleid='" + title.toStringId()
|
||||
|
@ -96,6 +96,10 @@ define(function(require) {
|
||||
distance: function(p) {
|
||||
return line(this, p).length();
|
||||
},
|
||||
// Returns the bearing between me and point 'p'
|
||||
bearing: function(p) {
|
||||
return line(this, p).bearing();
|
||||
},
|
||||
// Returns a manhattan (taxi-cab) distance between me and point `p`.
|
||||
manhattanDistance: function(p) {
|
||||
return abs(p.x - this.x) + abs(p.y - this.y);
|
||||
@ -245,6 +249,31 @@ define(function(require) {
|
||||
}
|
||||
return point(this.start.x + (alpha * pt1Dir.x / det),
|
||||
this.start.y + (alpha * pt1Dir.y / det));
|
||||
},
|
||||
/**
|
||||
* Returns the bearing (cardinal direction) of the line. For example N, W, or SE
|
||||
* @returns {String} One of the following bearings : NE, E, SE, S, SW, W, NW, N
|
||||
*/
|
||||
bearing: function() {
|
||||
var lat1 = this.start.x * Math.PI / 180;
|
||||
var lat2 = this.end.x * Math.PI / 180;
|
||||
var lon1 = this.start.y * Math.PI / 180;
|
||||
var lon2 = this.end.y * Math.PI / 180;
|
||||
var dLon = lon2 - lon1;
|
||||
var y = Math.sin(dLon) * Math.cos(lat2);
|
||||
var x = Math.cos(lat1) * Math.sin(lat2) -
|
||||
Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon);
|
||||
var brng = Math.atan2(y, x) / Math.PI * 180;
|
||||
|
||||
var bearings = ["NE", "E", "SE", "S", "SW", "W", "NW", "N"];
|
||||
|
||||
var index = brng - 22.5;
|
||||
if (index < 0)
|
||||
index += 360;
|
||||
index = parseInt(index / 45);
|
||||
|
||||
return(bearings[index]);
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user