aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/node
diff options
context:
space:
mode:
authorGravatar Jan Tattermusch <jtattermusch@google.com>2018-03-22 11:54:28 +0100
committerGravatar Jan Tattermusch <jtattermusch@google.com>2018-03-23 07:44:30 +0100
commita26aecc03b5c110ce0912f3340933a4ed87d5ec0 (patch)
treefd54656fc841298d4f8908972f8c4ce62396c30a /examples/node
parentb4a5727149201bac53a33c53c2cf93fed5414540 (diff)
reimplement distance calculation in routeguide
Diffstat (limited to 'examples/node')
-rw-r--r--examples/node/dynamic_codegen/route_guide/route_guide_server.js27
-rw-r--r--examples/node/static_codegen/route_guide/route_guide_server.js27
2 files changed, 24 insertions, 30 deletions
diff --git a/examples/node/dynamic_codegen/route_guide/route_guide_server.js b/examples/node/dynamic_codegen/route_guide/route_guide_server.js
index ab537ff401..f9028e860d 100644
--- a/examples/node/dynamic_codegen/route_guide/route_guide_server.js
+++ b/examples/node/dynamic_codegen/route_guide/route_guide_server.js
@@ -103,7 +103,7 @@ function listFeatures(call) {
/**
* Calculate the distance between two points using the "haversine" formula.
- * This code was taken from http://www.movable-type.co.uk/scripts/latlong.html.
+ * The formula is based on http://mathforum.org/library/drmath/view/51879.html.
* @param start The starting point
* @param end The end point
* @return The distance between the points in meters
@@ -112,21 +112,18 @@ function getDistance(start, end) {
function toRadians(num) {
return num * Math.PI / 180;
}
- var lat1 = start.latitude / COORD_FACTOR;
- var lat2 = end.latitude / COORD_FACTOR;
- var lon1 = start.longitude / COORD_FACTOR;
- var lon2 = end.longitude / COORD_FACTOR;
- var R = 6371000; // metres
- var φ1 = toRadians(lat1);
- var φ2 = toRadians(lat2);
- var Δφ = toRadians(lat2-lat1);
- var Δλ = toRadians(lon2-lon1);
-
- var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
- Math.cos(φ1) * Math.cos(φ2) *
- Math.sin(Δλ/2) * Math.sin(Δλ/2);
+ var R = 6371000; // earth radius in metres
+ var lat1 = toRadians(start.latitude / COORD_FACTOR);
+ var lat2 = toRadians(end.latitude / COORD_FACTOR);
+ var lon1 = toRadians(start.longitude / COORD_FACTOR);
+ var lon2 = toRadians(end.longitude / COORD_FACTOR);
+
+ var deltalat = lat2-lat1;
+ var deltalon = lon2-lon1;
+ var a = Math.sin(deltalat/2) * Math.sin(deltalat/2) +
+ Math.cos(lat1) * Math.cos(lat2) *
+ Math.sin(dlon/2) * Math.sin(dlon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
-
return R * c;
}
diff --git a/examples/node/static_codegen/route_guide/route_guide_server.js b/examples/node/static_codegen/route_guide/route_guide_server.js
index ef00bbbdfb..eecac62354 100644
--- a/examples/node/static_codegen/route_guide/route_guide_server.js
+++ b/examples/node/static_codegen/route_guide/route_guide_server.js
@@ -102,7 +102,7 @@ function listFeatures(call) {
/**
* Calculate the distance between two points using the "haversine" formula.
- * This code was taken from http://www.movable-type.co.uk/scripts/latlong.html.
+ * The formula is based on http://mathforum.org/library/drmath/view/51879.html.
* @param start The starting point
* @param end The end point
* @return The distance between the points in meters
@@ -111,21 +111,18 @@ function getDistance(start, end) {
function toRadians(num) {
return num * Math.PI / 180;
}
- var lat1 = start.getLatitude() / COORD_FACTOR;
- var lat2 = end.getLatitude() / COORD_FACTOR;
- var lon1 = start.getLongitude() / COORD_FACTOR;
- var lon2 = end.getLongitude() / COORD_FACTOR;
- var R = 6371000; // metres
- var φ1 = toRadians(lat1);
- var φ2 = toRadians(lat2);
- var Δφ = toRadians(lat2-lat1);
- var Δλ = toRadians(lon2-lon1);
-
- var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
- Math.cos(φ1) * Math.cos(φ2) *
- Math.sin(Δλ/2) * Math.sin(Δλ/2);
+ var R = 6371000; // earth radius in metres
+ var lat1 = toRadians(start.getLatitude() / COORD_FACTOR);
+ var lat2 = toRadians(end.getLatitude() / COORD_FACTOR);
+ var lon1 = toRadians(start.getLongitude() / COORD_FACTOR);
+ var lon2 = toRadians(end.getLongitude() / COORD_FACTOR);
+
+ var deltalat = lat2-lat1;
+ var deltalon = lon2-lon1;
+ var a = Math.sin(deltalat/2) * Math.sin(deltalat/2) +
+ Math.cos(lat1) * Math.cos(lat2) *
+ Math.sin(deltalon/2) * Math.sin(deltalon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
-
return R * c;
}