【发布时间】:2018-03-20 09:52:37
【问题描述】:
我正在尝试计算坐标之间的直线距离。我可以使用硬编码值成功地做到这一点,但是当我有一个坐标列表时我的问题就出现了。 S 基本上第一组坐标将始终是我可以成功获得的当前位置。第二组坐标来自 JSON 文件,如下所示。
[
{
"LocationID": 407,
"LocationName": "Free State",
"CustomerName": "Build It - Botshabelo ",
"ContactNo": "051 - 534 4072",
"Address": "75 Blue Street, Botshabelo",
"Zipcode": null,
"Longitude": "26,704671",
"Latitude": "-29,199533",
"Status": 1,
"CreatedDate": "25:29,0",
"CreatedBy": 1,
"ModifiedDate": "25:29,0",
"ModifiedBy": 1,
"ShopType": "Retails",
"Region": null,
"FIELD16": "Closed "
},
{
"LocationID": 408,
"LocationName": "Free State",
"CustomerName": "Cashbuild - Thabanchu",
"ContactNo": "051 - 875 1590",
"Address": "Brug St, Thaba Nchu",
"Zipcode": null,
"Longitude": "26,779109",
"Latitude": "-29,196689",
"Status": 1,
"CreatedDate": "25:29,0",
"CreatedBy": 1,
"ModifiedDate": "25:29,0",
"ModifiedBy": 1,
"ShopType": "Retails",
"Region": null,
"FIELD16": "Closed "
},
{
"LocationID": 409,
"LocationName": "Free State",
"CustomerName": "Ladybrand Mica",
"ContactNo": "082 - 568 5387",
"Address": "17 Joubert St, Ladybrand",
"Zipcode": null,
"Longitude": "27,458835",
"Latitude": "-29,191979",
"Status": 1,
"CreatedDate": "25:29,0",
"CreatedBy": 1,
"ModifiedDate": "25:29,0",
"ModifiedBy": 1,
"ShopType": "Retails",
"Region": null,
"FIELD16": ""
}
]
我正在遍历数组以获取纬度和经度
this.http.get('assets/stores.json').subscribe(data => {
this.places = data;
for(var i = 0; i < this.places.length; i++){
this.placePOS = this.places[i].Latitude, this.places[i].Longitude;
var distance = this.distance(this.currentLat, this.currentLon, this.places[i].Latitude, this.places[i].Longitude, 'K');
}
});
距离函数在这里创建
distance(lat1, lon1, lat2, lon2, unit) {
var radlat1 = Math.PI * lat1/180
var radlat2 = Math.PI * lat2/180
var radlon1 = Math.PI * lon1/180
var radlon2 = Math.PI * lon2/180
var theta = lon1-lon2
var radtheta = Math.PI * theta/180
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
dist = Math.acos(dist)
dist = dist * 180/Math.PI
dist = dist * 60 * 1.1515
if (unit=="K") { dist = dist * 1.609344 }
if (unit=="N") { dist = dist * 0.8684 }
return dist
}
当我运行代码时,我得到一个 NaN。请帮忙
【问题讨论】:
-
I get a NaN.哪里,哪一行? -
NaN 在这一行的 for 循环中 var distance = ....
-
好的,等等,如果我查看 JSON 文件,lat 和 lon 包含在 "" 中,并且距离函数需要一个不带 "" 的值。这可能会导致问题
标签: javascript ionic-framework coordinates haversine