From a26aecc03b5c110ce0912f3340933a4ed87d5ec0 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Thu, 22 Mar 2018 11:54:28 +0100 Subject: reimplement distance calculation in routeguide --- .../route_guide/route_guide_server.js | 27 ++++++++++------------ .../route_guide/route_guide_server.js | 27 ++++++++++------------ 2 files changed, 24 insertions(+), 30 deletions(-) (limited to 'examples/node') 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; } -- cgit v1.2.3