在JavaScript中将度数(0-360)从一个LatLng获取到另一个LatLng [英] Get Degrees (0-360) from One LatLng to Another in JavaScript
问题描述
函数getHeading(lat1,lon1,lat2,lon2)有人可以帮助我用原生JavaScript填充空白吗? {
//在这里用数学做一些很酷的事情
return heading; //应该是0到360之间的数字
}
我一直在搞乱这很长一段时间,似乎无法让我的代码正常工作。
有一个非常好的JavaScript Chris Veness执行计算纬度/经度点之间的距离,方位和更多在轴承标题下。
您可能更喜欢用 getHeading
来扩充Google的 LatLng
>方法,如下所示(使用 v3 API ):
Number.prototype.toRad = function(){
return this * Math.PI / 180;
}
Number.prototype.toDeg = function(){
return this * 180 / Math.PI;
google.maps.LatLng.prototype.getHeading = function(point){
var lat1 = this.lat()。toRad(),lat2 = point.lat ().toRad();
var dLon =(point.lng() - this.lng())。toRad();
var y = Math.sin(dLon)* Math.cos(lat2);
var x = Math.cos(lat1)* Math.sin(lat2) -
Math.sin(lat1)* Math.cos(lat2)* Math.cos(dLon);
var brng = Math.atan2(y,x);
return((brng.toDeg()+ 360)%360);
}
然后可以这样使用:
var pointA = new google.maps.LatLng(40.70,-74.00);
var pointB = new google.maps.LatLng(40.70,-75.00);
pointA.getHeading(pointB); //返回270度
否则,如果您更喜欢使用全局函数而不是增加Google的 LatLng
,你可以这样做:
function getHeading(lat1,lon1,lat2, lon2){
var lat1 = lat1 * Math.PI / 180;
var lat2 = lat2 * Math.PI / 180;
var dLon =(lon2 - lon1)* Math.PI / 180;
var y = Math.sin(dLon)* Math.cos(lat2);
var x = Math.cos(lat1)* Math.sin(lat2) -
Math.sin(lat1)* Math.cos(lat2)* Math.cos(dLon);
var brng = Math.atan2(y,x);
return(((brng * 180 / Math.PI)+ 360)%360);
}
用法:
getHeading(40.70,-74.00,40.70,-75.00); //返回270度
Can someone help me fill in the blanks with native JavaScript?
function getHeading(lat1, lon1, lat2, lon2) {
// Do cool things with math here
return heading; // Should be a number between 0 and 360
}
I've been messing around with this for a long time and can't seem to get my code to work right.
There is a very good JavaScript implementation by Chris Veness at Calculate distance, bearing and more between Latitude/Longitude points under the Bearings heading.
You may prefer augmenting Google's LatLng
prototype with a getHeading
method, as follows (using the v3 API):
Number.prototype.toRad = function() {
return this * Math.PI / 180;
}
Number.prototype.toDeg = function() {
return this * 180 / Math.PI;
}
google.maps.LatLng.prototype.getHeading = function(point) {
var lat1 = this.lat().toRad(), lat2 = point.lat().toRad();
var dLon = (point.lng() - this.lng()).toRad();
var y = Math.sin(dLon) * Math.cos(lat2);
var x = Math.cos(lat1) * Math.sin(lat2) -
Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon);
var brng = Math.atan2(y, x);
return ((brng.toDeg() + 360) % 360);
}
Which then can be used like this:
var pointA = new google.maps.LatLng(40.70, -74.00);
var pointB = new google.maps.LatLng(40.70, -75.00);
pointA.getHeading(pointB); // Returns 270 degrees
Otherwise if you prefer a global function instead of augmenting Google's LatLng
, you can do it as follows:
function getHeading(lat1, lon1, lat2, lon2) {
var lat1 = lat1 * Math.PI / 180;
var lat2 = lat2 * Math.PI / 180;
var dLon = (lon2 - lon1) * Math.PI / 180;
var y = Math.sin(dLon) * Math.cos(lat2);
var x = Math.cos(lat1) * Math.sin(lat2) -
Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon);
var brng = Math.atan2(y, x);
return (((brng * 180 / Math.PI) + 360) % 360);
}
Usage:
getHeading(40.70, -74.00, 40.70, -75.00); // Returns 270 degrees
这篇关于在JavaScript中将度数(0-360)从一个LatLng获取到另一个LatLng的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!