eav*_*eav 19 math location geolocation direction compass-geolocation
我正在为移动设备制作一个"指南针".我有以下几点:
point 1 (current location): Latitude = 47.2246, Longitude = 8.8257
point 2 (target location): Latitude = 50.9246, Longitude = 10.2257
Run Code Online (Sandbox Code Playgroud)
另外我有以下信息(来自我的android手机):
The compass-direction in degree, wich bears to the north.
For example, when I direct my phone to north, I get 0°
Run Code Online (Sandbox Code Playgroud)
我怎样才能创建一个"指南针"箭头,向我展示指向方向的方向?
这有数学问题吗?
谢谢!
编辑:好的,我找到了一个解决方案,它看起来像这样:
/**
* 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;
}
Run Code Online (Sandbox Code Playgroud)
这对我很有用!
efw*_*mes 16
哦忘了说我最终找到了答案.该应用程序用于确定运输车辆的罗盘方向及其目的地.基本上,花式数学用于获取地球曲率,找到角度/罗盘读数,然后将该角度与通用罗盘值匹配.你当然可以保留compassReading并将其作为图像的旋转量应用.请注意,这是到终点(公交车站)的车辆方向的平均确定,这意味着它无法知道道路在做什么(所以这可能最适用于飞机或轮滑德比).
//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
}
Run Code Online (Sandbox Code Playgroud)
即:vehicleBearing(mybus,busstation)可能返回"NW"意味着它向西北方向行进