计算当前坐标和坐标列表之间的距离

时间:2018-03-20 09:52:37

标签: javascript ionic-framework coordinates haversine

我正在尝试计算坐标之间的直线距离。我可以使用硬编码值成功地完成此操作,但是当我有一个坐标列表时,我的问题就出现了。 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。请协助

4 个答案:

答案 0 :(得分:3)

看到没有其他人发现它,..而且@eohdev现在已经离开了,认为结果是正确的,只是因为他们现在没有给NaN,我会发布问题的主要原因。

parseFloat是一种很好的做法,但它不是导致问题的原因.Javascript通常可以自动输出类型...例如。即使"20" / 4是字符串,5也会等于20"20" - "5"等于15, JS无法自动解决的问题是"20" + "5"这将是205 ..这是因为你可以连接字符串,并添加数字以便它不能自动选择,这就是为什么parseFloat等是一个好的想法。

但回到这里的问题,它是源数据.. "Longitude": "27,458835" 该数字不是有效的浮点数,JS中的浮点数没有逗号。

那么为什么看起来parseFloat就像你问的那样......好parseFloat("27,458835")会导致27 ..哦,亲爱的..除非你的距离计算被截断到最接近的程度,这可能不是你的追求。

所以问题不是parseFloat丢失,但是源数据使用逗号,而不是完全停止它的小数。因此,如果您将逗号更改为完全停止,则当前代码将起作用,但我也会说保留parseFloats也是一个很好的措施。

答案 1 :(得分:2)

对于我知道你知道 this.places [i] .Latitude 的值这不是一个数字所以在你将它发送给一个数字之前需要将它转换成一个数字。功能或功能内。

使用 Number()或使用 parseFloat()

正如@Keith针对 parseFloat()所指出的,两个会话都无法处理原始JSON对象中的逗号。 Number()将再次生成NaN, parseFloat()将数字四舍五入到最接近的完整数字。因此,有必要用点替换JSON对象中的逗号。

感谢@Keith

答案 2 :(得分:1)

当您尝试对非数字的变量执行某些数学运算时,您会得到NaN。 在您的问题中,您拥有的文档数组,纬度和经度是字符串格式,并且您不会在任何地方解析浮点数。首先将这些字符串转换为数字,然后根据需要进行数学运算。

PS:我可以给你代码,但如果你自己尝试一下会更好。

答案 3 :(得分:0)

你在距离功能方面遇到了问题。你需要使用parseFloat代替lon(s)和lat(s)将它们转换成字符串中的数字。

我试着改变你的代码。 https://jshttps://jsfiddle.net/startsco/z5n7q2zn/10/fiddle.net/startsco/z5n7q2zn/10/

运行它并检查你的控制台。