我正在研究指南针"用于移动设备。我有以下几点:
point 1 (current location): Latitude = 47.2246, Longitude = 8.8257
point 2 (target location): Latitude = 50.9246, Longitude = 10.2257
此外,我还有以下信息(来自我的Android手机):
The compass-direction in degree, wich bears to the north.
For example, when I direct my phone to north, I get 0°
我如何创建一个类似罗盘的"箭头向我展示了指向的方向?
这是否存在数学问题?
谢谢!
编辑:好的,我找到了一个解决方案,它看起来像这样:/**
* Params: lat1, long1 => Latitude and Longitude of current point
* lat2, long2 => Latitude and Longitude of target point
*
* headX => x-Value of built-in phone-compass
*
* Returns the degree of a direction from current point to target point
*
*/
function getDegrees(lat1, long1, lat2, long2, headX) {
var dLat = toRad(lat2-lat1);
var dLon = toRad(lon2-lon1);
lat1 = toRad(lat1);
lat2 = toRad(lat2);
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 = toDeg(Math.atan2(y, x));
// fix negative degrees
if(brng<0) {
brng=360-Math.abs(brng);
}
return brng - headX;
}
这对我很有用!
答案 0 :(得分:16)
//example obj data containing lat and lng points
//stop location - the radii end point
endpoint.lat = 44.9631;
endpoint.lng = -93.2492;
//bus location from the southeast - the circle center
startpoint.lat = 44.95517;
startpoint.lng = -93.2427;
function vehicleBearing(endpoint, startpoint) {
endpoint.lat = x1;
endpoint.lng = y1;
startpoint.lat = x2;
startpoint.lng = y2;
var radians = getAtan2((y1 - y2), (x1 - x2));
function getAtan2(y, x) {
return Math.atan2(y, x);
};
var compassReading = radians * (180 / Math.PI);
var coordNames = ["N", "NE", "E", "SE", "S", "SW", "W", "NW", "N"];
var coordIndex = Math.round(compassReading / 45);
if (coordIndex < 0) {
coordIndex = coordIndex + 8
};
return coordNames[coordIndex]; // returns the coordinate value
}
即: vehicleBearing(mybus,busstation) 可能会回归&#34; NW&#34;意味着它向西北方向旅行
答案 1 :(得分:1)
我在数学here中找到了一些有用的gps坐标公式。 在这种情况下,这是我的解决方案
private double getDirection(double lat1, double lng1, double lat2, double lng2) {
double PI = Math.PI;
double dTeta = Math.log(Math.tan((lat2/2)+(PI/4))/Math.tan((lat1/2)+(PI/4)));
double dLon = Math.abs(lng1-lng2);
double teta = Math.atan2(dLon,dTeta);
double direction = Math.round(Math.toDegrees(teta));
return direction; //direction in degree
}
答案 2 :(得分:0)
我无法理解你的解决方案,计算适合我的斜率。 修改efwjames和你的答案。这应该做 -
// Error: AttributeError: file <string> line 88: 'exceptions.RuntimeError' object has no attribute 'errno' //
// Warning: Python callback failed //
答案 3 :(得分:-2)
你需要计算起点和终点之间的欧几里得向量,然后计算它的角度(假设相对于正X),这是你想要旋转箭头的角度。