geolocation - 我想计算离子 4 中两个位置之间的距离(以 km 为单位)。我使用了一个给我错误的函数
问题描述
distance(lon1, lat1, lon2, lat2) {
var R = 6371; // Radius of the earth in km
var dLat = (lat2-lat1).toRad(); // Javascript functions in radians
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; // Distance in km
return d;
}
如果我使用这个函数,我使用 toRad() 函数。每当我使用它时,它都会报错:
属性 toRad() 不存在类型号。
请帮助我是什么原因是我必须导入一些东西?
解决方案
您应该像这样编写自己的toRad
函数:
function toRad(Value) {
return Value * Math.PI / 180;
}
然后以功能方式使用它:
function distance(lon1, lat1, lon2, lat2) {
var R = 6371;
var dLat = toRad(lat2 - lat1);
var dLon = toRad(lon2 - lon1);
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;
return d;
}
顺便说一句,你是用 Ionic Angular 编写你的应用程序吗?你不应该使用 Typescript 而不是 JavaScript 吗?
推荐阅读
- sql - Presto 数组包含一个喜欢某种模式的元素
- c# - 计算网格上的最短路径
- javascript - $.getJSON 在 for 循环中没有 let 语句以实现浏览器兼容性
- acl - Syslog 用户能够通过 ACL 读取文件,但 rsyslog 进程不是
- mysql - 在ubuntu中安装xampp后首次打开phpmyadmin时phpmyadmin没有连接
- c# - 方法阻塞 UI 线程
- c# - 修改文件权限 .NET 适用于 Windows 10,但不适用于 Server 2008
- php - php simplexml 在两个 xml 文档之间查找/替换
- sql - 基于当天的查询条件(周六/周日应该返回周五的值)
- html - 如何编写一个仅在屏幕垂直翻转时出现在移动设备上并在水平翻转时消失的弹出文本框?