AngularJS获取多个坐标之间的距离
AngularJS Getting distance between multiple coordinates
我写了一个应用程序,有大量的位置,所有与坐标,我希望应用程序能够搜索的位置名称,或由哪一个是最近的。我知道如何通过哈弗斯公式来确定两个坐标之间的距离。然而,我希望我的应用程序通过每个对象(位置)在一个数组中,并添加一个新的元素到该对象称为distance
和distance
应该是从我的位置到位置的距离。我有下面的代码,我认为应该工作,但它没有。有人有什么想法吗?
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'}))
相关文章:
- 铯JS-两点之间的距离
- cloudSearch:范围从0到某个值之间的距离
- 查找图像上两点之间的距离,即使缩放图像也是如此
- 如何使用谷歌地图几何库查找两点之间的距离
- HTML5 画布绘制点之间的线条距离
- javascript中两个位置之间的距离
- 如何使用JavaScript设置内容和页脚之间的距离
- 使用javascript计算两个邮政编码之间的距离
- 只需显示两点之间的距离(KM)
- 计算标记之间的距离
- d3.js中条形图之间的相关距离
- 在强制布局 D3.js 中保持节点之间的动态链接距离
- 如何计算两个纬度和经度之间的距离
- X 轴标签与高图表中的图表之间的距离
- DOM 中两个元素之间的距离(以 px 为单位)
- 删除云中点之间的间隙 - 随着距离的靠近,使它们变大
- 搜索的地址与谷歌地图 api v3 上最近的现有地标之间的距离
- D3 力布局:如何保持节点之间的给定最小距离
- 坐标之间的音差地理位置距离
- 计算两个邮编之间距离的最佳方法(英国)