经纬度海拔计算距离js
时间: 2023-08-30 09:08:41 浏览: 55
您好!计算两点间的距离可以使用经纬度坐标计算,可以采用 Haversine 公式,公式如下:
```
var R = 6371; // 地球半径,单位为千米
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad();
var lat1 = lat1.toRad();
var lat2 = lat2.toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1) * Math.cos(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; // 两点间距离,单位为千米
```
其中 `toRad()` 是将角度转换为弧度的函数,可以定义如下:
```
if (typeof(Number.prototype.toRad) === "undefined") {
Number.prototype.toRad = function() {
return this * Math.PI / 180;
}
}
```
如果您需要计算两点间的海拔高度差,可以使用 Google Elevation API,具体使用方法可以参考官方文档:https://developers.google.com/maps/documentation/elevation/start