发出两个GPS坐标之间的计算指南针 [英] Issue with calcuating compass bearing between two GPS coordinates
本文介绍了发出两个GPS坐标之间的计算指南针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的web应用程序中,从包含1到n位置的经纬度坐标的数据库查询中获取JSON数据响应。我想计算从 data [i]
位置到当前位置的方位。
我已经修改此处的代码,但返回的方向不正确。
//开始lat / long,并将lat转换为rads
var endLat = toRad(location.lat());
var endLong = location.lng();
//循环响应,计算链接的新标题并添加链接到数组
for(var i = 0; i< data.length; i ++){
//此链接的纬度/经度坐标,将lat转换为rads
var startLat = toRad(data [i] .lat);
var startLong = data [i] .lon;
//获得以rads开始和结束坐标之间的delta值
var dLong = toRad(endLong - startLong);
//计算
var y = Math.sin(dLong)* Math.cos(endLong);
var x = Math.cos(startLat)* Math.sin(endLat)-Math.sin(startLat)* Math.cos(endLat)* Math.cos(dLong);
var bearing = Math.atan(y,x);
bearing =(toDeg(bearing)+ 360)%360; $ {
panoLinks.push({'heading':bearing,'description':data [i] .description,'pano':data [i] .description});
}
//弧度/度转换
函数toRad(转换){
返回转换* Math.PI / 180;
}
函数toDeg(convert){
return convert * 180 / Math.PI;
$ / code>
使用上面的函数和值
startLat = 43.6822,转换为0.7623982145146669弧度
startLong = -70.450769
endLat = 43.682211,转换为0.7623984065008848弧度
endLong = -70.45070
dLong = startLong - endLong,转换为0.0000011170107216805305弧度
导致指南针的程度为
bearing = 0.000014910023935499339
这绝对是关闭的。
解决方案
试试这个,我不能为了我的生活记得我在哪里尽管...
/ **
*计算两个位置之间的方位为0-360
*
* @param lat1 - 第一个位置的纬度
* @param lng1 - 第一个位置的经度
* @param lat2 - 第二个位置的纬度
* @param lng2 - 第二个位置的经度
*
* @return int - 0到360之间的方位
* /
方位:函数(lat1, lng1,lat2,lng2){
var dLon =(lng2-lng1);
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 = this._toDeg(Math.atan2(y,x));
return 360 - ((brng + 360)%360);
$ **
*由于不是所有的浏览器都实现这个功能,我们有我们自己的实用程序,将
*从度数转换为弧度
*
* @param deg - 要转换为弧度的度数
* @return弧度
* /
_toRad:函数(deg){
return deg * Math。 PI / 180;
$ **
*由于不是所有的浏览器都实现这个功能,我们有自己的实用程序,将
*从弧度转换为度数
*
* @param rad - 要转换为度数的弧度
* @返回度
* /
_toDeg:函数(rad){
返回弧度* 180 / Math.PI;
},
In my webapp, have a JSON data response from a database query that includes the lat/long coordinates of 1 to n locations. I want to calculate the bearing from the data[i]
location to the current position.
I've been adapting the code here, but the bearing returned is incorrect.
//starting lat/long along with converting lat to rads
var endLat = toRad(location.lat());
var endLong = location.lng();
//loop over response, calculate new headings for links and add link to array
for(var i=0; i<data.length; i++){
//this link's lat/long coordinates, convert lat to rads
var startLat = toRad(data[i].lat);
var startLong = data[i].lon;
//get the delta values between start and end coordinates in rads
var dLong = toRad(endLong - startLong);
//calculate
var y = Math.sin(dLong)*Math.cos(endLong);
var x = Math.cos(startLat)*Math.sin(endLat)-Math.sin(startLat)*Math.cos(endLat)*Math.cos(dLong);
var bearing = Math.atan(y, x);
bearing = (toDeg(bearing) + 360) % 360;
panoLinks.push({'heading': bearing, 'description': data[i].description, 'pano': data[i].description});
}
//radian/degree conversions
function toRad(convert){
return convert * Math.PI/180;
}
function toDeg(convert){
return convert * 180/Math.PI;
}
Using the functions above and the values
startLat= 43.6822, converts to 0.7623982145146669 radians
startLong= -70.450769
endLat= 43.682211, converts to 0.7623984065008848 radians
endLong= -70.45070
dLong = startLong - endLong, converts to 0.0000011170107216805305 radians
results in a compass degree of
bearing= 0.000014910023935499339
which is definitely off. Where have I gone wrong?
解决方案
Give this a try, I can't for the life of me remember where I got it though...
/**
* Calculate the bearing between two positions as a value from 0-360
*
* @param lat1 - The latitude of the first position
* @param lng1 - The longitude of the first position
* @param lat2 - The latitude of the second position
* @param lng2 - The longitude of the second position
*
* @return int - The bearing between 0 and 360
*/
bearing : function (lat1,lng1,lat2,lng2) {
var dLon = (lng2-lng1);
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 = this._toDeg(Math.atan2(y, x));
return 360 - ((brng + 360) % 360);
},
/**
* Since not all browsers implement this we have our own utility that will
* convert from degrees into radians
*
* @param deg - The degrees to be converted into radians
* @return radians
*/
_toRad : function(deg) {
return deg * Math.PI / 180;
},
/**
* Since not all browsers implement this we have our own utility that will
* convert from radians into degrees
*
* @param rad - The radians to be converted into degrees
* @return degrees
*/
_toDeg : function(rad) {
return rad * 180 / Math.PI;
},
这篇关于发出两个GPS坐标之间的计算指南针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文