Javascript 在Javascript中使用Haversine公式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14560999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:12:16  来源:igfitidea点击:

Using the Haversine Formula in Javascript

javascripthaversine

提问by Creights

I'm trying to use the Haversine Distance Formula (as found here: http://www.movable-type.co.uk/scripts/latlong.html) but I can't get it to work, please see the following code

我正在尝试使用Haversine距离公式(如在此处找到:http: //www.movable-type.co.uk/scripts/latlong.html)但我无法让它工作,请参阅以下代码

    function test() { 
    var lat2 = 42.741; 
    var lon2 = -71.3161; 
    var lat1 = 42.806911; 
    var lon1 = -71.290611; 

    var R = 6371; // km 
    //has a problem with the .toRad() method below.
    var dLat = (lat2-lat1).toRad();  
    var dLon = (lon2-lon1).toRad();  
    var a = Math.sin(dLat/2) * Math.sin(dLat/2) + 
                    Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
                    Math.sin(dLon/2) * Math.sin(dLon/2);  
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    var d = R * c; 

    alert(d); 
}

And the error is:

错误是:

Uncaught TypeError: Object -0.06591099999999983 has no method 'toRad' 

Which I understand to be because it needs to do the following:

我理解是因为它需要执行以下操作:

Number.prototype.toRad = function() {
return this * Math.PI / 180;
}

But when I put this below the function, it still comes back with the same error message. How do I make it use the helper method? Or is there an alternative way to code this to get it to work? Thanks!

但是当我把它放在函数下面时,它仍然返回相同的错误消息。我如何使它使用辅助方法?或者是否有另一种方法来编码它以使其工作?谢谢!

回答by talkol

This code is working:

此代码正在工作:

Number.prototype.toRad = function() {
   return this * Math.PI / 180;
}

var lat2 = 42.741; 
var lon2 = -71.3161; 
var lat1 = 42.806911; 
var lon1 = -71.290611; 

var R = 6371; // km 
//has a problem with the .toRad() method below.
var x1 = lat2-lat1;
var dLat = x1.toRad();  
var x2 = lon2-lon1;
var dLon = x2.toRad();  
var a = Math.sin(dLat/2) * Math.sin(dLat/2) + 
                Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
                Math.sin(dLon/2) * Math.sin(dLon/2);  
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
var d = R * c; 

alert(d);

Notice how I defined x1 and x2. Play with it at: https://tinker.io/3f794

注意我是如何定义 x1 和 x2 的。玩它:https: //tinker.io/3f794

回答by Nathan Lippi

Here's a refactored function based on 3 of the other answers!

这是基于其他 3 个答案的重构函数!

Please note that the coords arguments are [longitude, latitude].

请注意,坐标参数是 [longitude, latitude]。

function haversineDistance(coords1, coords2, isMiles) {
  function toRad(x) {
    return x * Math.PI / 180;
  }

  var lon1 = coords1[0];
  var lat1 = coords1[1];

  var lon2 = coords2[0];
  var lat2 = coords2[1];

  var R = 6371; // km

  var x1 = lat2 - lat1;
  var dLat = toRad(x1);
  var x2 = lon2 - lon1;
  var dLon = toRad(x2)
  var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *
    Math.sin(dLon / 2) * Math.sin(dLon / 2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  var d = R * c;

  if(isMiles) d /= 1.60934;

  return d;
}

回答by talkol

Why not try the straight forward solution? Instead of extending Number prototype, just define toRad as a regular function:

为什么不尝试直接的解决方案?无需扩展 Number 原型,只需将 toRad 定义为常规函数即可:

function toRad(x) {
   return x * Math.PI / 180;
}

and then call toRadeverywhere:

然后toRad到处调用:

var dLat = toRad(lat2-lat1); 

Extending the Number prototype does not always work as expected. For example calling 123.toRad() does not work. I think that if you do var x1 = lat2 - lat1; x1.toRad();works better than doing (lat2-lat1).toRad()

扩展 Number 原型并不总是按预期工作。例如调用 123.toRad() 不起作用。我认为如果你做的var x1 = lat2 - lat1; x1.toRad();比做的更好(lat2-lat1).toRad()

回答by Harry Mumford-Turner

ES6 JavaScript/NodeJS refactored version:

ES6 JavaScript/NodeJS 重构版:

   /**
     * Calculates the haversine distance between point A, and B.
     * @param {number[]} latlngA [lat, lng] point A
     * @param {number[]} latlngB [lat, lng] point B
     * @param {boolean} isMiles If we are using miles, else km.
     */
    const haversineDistance = ([lat1, lon1], [lat2, lon2], isMiles = false) => {
      const toRadian = angle => (Math.PI / 180) * angle;
      const distance = (a, b) => (Math.PI / 180) * (a - b);
      const RADIUS_OF_EARTH_IN_KM = 6371;

      const dLat = distance(lat2, lat1);
      const dLon = distance(lon2, lon1);

      lat1 = toRadian(lat1);
      lat2 = toRadian(lat2);

      // Haversine Formula
      const a =
        Math.pow(Math.sin(dLat / 2), 2) +
        Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2);
      const c = 2 * Math.asin(Math.sqrt(a));

      let finalDistance = RADIUS_OF_EARTH_IN_KM * c;

      if (isMiles) {
        finalDistance /= 1.60934;
      }

      return finalDistance;
    };

See codepen for tests against accepted answer: https://codepen.io/harrymt/pen/dyYvLpJ?editors=1011

有关已接受答案的测试,请参阅 codepen:https://codepen.io/harrymt/pen/dyYvLpJ?editors=1011

回答by DanSingerman

You need to extend the Number prototype, before calling those extensions in a function.

在函数中调用这些扩展之前,您需要扩展 Number 原型。

So just ensure

所以只要确保

Number.prototype.toRad = function() {
  return this * Math.PI / 180;
}

is called beforeyour function is called.

在调用您的函数之前调用。

回答by Bergi

when I put this below the function

当我把它放在函数下面时

You only need to put it above the point where you call test(). Where the testfunction itself is declared does not matter.

你只需要把它放在你调用的点之上test()test函数本身在哪里声明并不重要。

回答by Dillon

Another variant to reduce redundancy and also compatible with Google LatLng objects:

另一种减少冗余并与 Google LatLng 对象兼容的变体:

  function haversine_distance(coords1, coords2) {

     function toRad(x) {
         return x * Math.PI / 180;
    }

  var dLat = toRad(coords2.latitude - coords1.latitude);
  var dLon = toRad(coords2.longitude - coords1.longitude)

  var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
          Math.cos(toRad(coords1.latitude)) * 
          Math.cos(toRad(coords2.latitude)) *
          Math.sin(dLon / 2) * Math.sin(dLon / 2);

  return 12742 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
}

回答by Perry Tew

This is a java implemetation of talkol's solution above. His or her solution worked very well for us. I'm not trying to answer the question, since the original question was for javascript. I'm just sharing our java implementation of the given javascript solution in case others find it of use.

这是上面talkol解决方案的java实现。他或她的解决方案对我们非常有效。我不是要回答这个问题,因为最初的问题是针对 javascript 的。我只是分享给定 javascript 解决方案的 java 实现,以防其他人发现它有用。

// this was a pojo class we used internally...
public class GisPostalCode {

    private String country;
    private String postalCode;
    private double latitude;
    private double longitude;

    // getters/setters, etc.
}


public static double distanceBetweenCoordinatesInMiles2(GisPostalCode c1, GisPostalCode c2) {

    double lat2 = c2.getLatitude();
    double lon2 = c2.getLongitude();
    double lat1 = c1.getLatitude();
    double lon1 = c1.getLongitude();

    double R = 6371; // km
    double x1 = lat2 - lat1;
    double dLat = x1 * Math.PI / 180;
    double x2 = lon2 - lon1;
    double dLon = x2 * Math.PI / 180;

    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(lat1*Math.PI/180) * Math.cos(lat2*Math.PI/180) *
        Math.sin(dLon/2) * Math.sin(dLon/2);

    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    double d = R * c;

    // convert to miles
    return d / 1.60934;
}

回答by Alessandro Braidotti

Here's another refactored answer in JavaScript:

这是 JavaScript 中另一个重构的答案:

getHaversineDistance = (firstLocation, secondLocation) => {
    const earthRadius = 6371; // km 

    const diffLat = (secondLocation.lat-firstLocation.lat) * Math.PI / 180;  
    const diffLng = (secondLocation.lng-firstLocation.lng) * Math.PI / 180;  

    const arc = Math.cos(
                    firstLocation.lat * Math.PI / 180) * Math.cos(secondLocation.lat * Math.PI / 180) 
                    * Math.sin(diffLng/2) * Math.sin(diffLng/2)
                    + Math.sin(diffLat/2) * Math.sin(diffLat/2);
    const line = 2 * Math.atan2(Math.sqrt(arc), Math.sqrt(1-arc));

    const distance = earthRadius * line; 

    return distance;
}

const philly = { lat: 39.9526, lng: -75.1652 }
const nyc = { lat: 40.7128, lng: -74.0060 }
const losAngeles = { lat: 34.0522, lng: -118.2437 }

console.log(getHaversineDistance(philly, nyc)) //129.61277152662188
console.log(getHaversineDistance(philly, losAngeles)) //3843.4534005980404