AngularJS获取多个坐标之间的距离

AngularJS Getting distance between multiple coordinates

本文关键字:之间 距离 坐标 获取 AngularJS      更新时间:2023-09-26

我写了一个应用程序,有大量的位置,所有与坐标,我希望应用程序能够搜索的位置名称,或由哪一个是最近的。我知道如何通过哈弗斯公式来确定两个坐标之间的距离。然而,我希望我的应用程序通过每个对象(位置)在一个数组中,并添加一个新的元素到该对象称为distancedistance应该是从我的位置到位置的距离。我有下面的代码,我认为应该工作,但它没有。有人有什么想法吗?

function GetLocation(location) {
    myLat = location.coords.latitude;
    myLon = location.coords.longitude;
    angular.forEach($scope.ASiteLocs, function(object, location) {
      location.Point.coordinates = location.Point.coordinates.substring(0, clength - 2).split(",");
      Lat = location.Point.coordinates[0];
      Lon = location.Point.coordinates[1];
      function getCoordDistance() {
        Number.prototype.toRad = function() {
          return this * Math.PI / 180;
        }
        var lat2 = myLat;
        var lon2 = myLon;
        var lat1 = Lat;
        var lon1 = Lon;
        var R = 3959; // Radius in miles 
        //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;
      }
      object.distance = d;
    });
  }

这里是位置的样子(只是其中的几个):

"name": "502 Nelson St, Greenville, MS 38701",
    "visibility": "0",
    "description": "502 Nelson St, Greenville, MS 38701",
    "styleUrl": "#waypoint",
    "Point": {
      "coordinates": "-91.05636,33.415485,0"
    }
  }, {
    "name": "242 Blackhawk Trace, Galena, IL 61036",
    "visibility": "0",
    "description": "242 Blackhawk Trace, Galena, IL 61036",
    "styleUrl": "#waypoint",
    "Point": {
      "coordinates": "-90.319778,42.390862,0"
    }
  }, {
    "name": "3747 Ocean Dr, Vero Beach, FL 32963",
    "visibility": "0",
    "description": "3747 Ocean Dr, Vero Beach, FL 32963",
    "styleUrl": "#waypoint",
    "Point": {
      "coordinates": "-80.358248,27.659094,0"
    }
  }

如果便于阅读,这里是我工作的一小部分。
http://plnkr.co/edit/nRQc7Ym0lsaK6jQwd626?p =预览

使用angular时。对于对象数组,函数的第一个参数是对象,第二个参数是数组索引:

angular.forEach($scope.ASiteLocs, function(location, arrayIndex) {
    ...
    location.distance = d;
})

注意1:你在forEach中将location. point .coordinates从字符串更改为数组,因此GetLocation函数只会工作一次,因为原始位置数组已更改。

注意2:在循环中定义函数是不好的做法,而且"代价很高"

如果你在你的代码中使用我的npm包haversine-calculator,你的代码会更干净:

const haversineCalculator = require('haversine-calculator')
const start = {
  latitude: -23.754842,
  longitude: -46.676781
}
const end = {
  latitude: -23.549588,
  longitude: -46.693210
}
console.log(haversineCalculator(start, end))
console.log(haversineCalculator(start, end, {unit: 'meter'}))
console.log(haversineCalculator(start, end, {unit: 'mile'}))
console.log(haversineCalculator(start, end, {threshold: 1}))
console.log(haversineCalculator(start, end, {threshold: 1, unit: 'meter'}))
console.log(haversineCalculator(start, end, {threshold: 1, unit: 'mile'}))