javascript - Google Maps Javascript - 计算航向和 compass 方向以计算面向方向

标签 javascript google-maps google-maps-api-3 compass direction

针对我的 currentPosition 和 destinationPosition 使用 computeHeading() 函数,我可以获得返回的 Angular (目前在 -180 和 +180 之间)。

heading = google.maps.geometry.spherical.computeHeading(
    currentLocation,
    destinationLocation
);

我还可以使用函数返回 alpha 来获取 compass 的方向,该函数提供了从北方旋转的 Angular 。

alpha = null;
//Check for iOS property
if (event.webkitCompassHeading) {
    //window.confirm("iOS device - using webKit instead"); // report back that we are indeed on iOS
    alpha = event.webkitCompassHeading;
}
//non iOS
else {
    alpha = event.alpha;
}

var locationIcon = myLocationMarker.get('icon');
locationIcon.rotation = 360 - alpha;
myLocationMarker.set('icon', locationIcon);

这给了我 Angular 然后帮助我旋转我的图标所以我可以看看我是否指向正确的方向

有人能告诉我数学/js 代码然后让我面对目的地的方式给我一个返回的结果。我需要知道我是否面向目的地,然后我可以查看我是否面向错误的方向等等。

我将尝试使用一些网络音频的声像来帮助引导人们指向正确的方向。

谢谢

编辑:这里有一张图片可能有助于澄清。我确定这是一个简单的计算,但我无法弄清楚 enter image description here

最佳答案

计算两个纬度/经度点之间的 Angular

private double angleFromCoordinate(double lat1, double long1, double lat2,
        double long2) {

    double dLon = (long2 - long1);

    double y = Math.sin(dLon) * Math.cos(lat2);
    double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
            * Math.cos(lat2) * Math.cos(dLon);

    double brng = Math.atan2(y, x);

    brng = Math.toDegrees(brng);
    brng = (brng + 360) % 360;
    brng = 360 - brng; // count degrees counter-clockwise - remove to make clockwise

    return brng;
}

============================================= ======================

使用 GoogleMap api:

<script src="https://maps.googleapis.com/maps/api/js?v=3&sensor=false&libraries=geometry"></script>

var point1 = new google.maps.LatLng(lat1, lng1);
var point2 = new google.maps.LatLng(lat2, lng2);
var heading = google.maps.geometry.spherical.computeHeading(point1,point2);

关于javascript - Google Maps Javascript - 计算航向和 compass 方向以计算面向方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30976061/

相关文章:

javascript - 将 onRender() 函数中创建的对象保存在 htmlWidgets 中

android - 使用 fragment 更改为横向时 map 行为会发生变化

css - 尽管垂直滚动如何保持背景 DIV 出现

javascript - 热访问谷歌地图对象以获取 LatLngToPoint

google-maps-api-3 - 谷歌地图 API V3 : Bound world to fit area and disable map repeat

javascript - 使用 Rails 3.1 Assets 管道将 Javascript 放在文件末尾

javascript - chrome扩展InjectDetails中文档的含义

javascript - famo.us 中的表面渲染事件

javascript - 谷歌地图地理编码无法在模态中工作

javascript - 在 Google Maps API v3 中切换平移和标记移动锁定