Get Degrees (0-360) from One LatLng to Another in JavaScript

后端 未结 1 2021
星月不相逢
星月不相逢 2021-01-07 02:39

Can someone help me fill in the blanks with native JavaScript?

function getHeading(lat1, lon1, lat2, lon2) {
    // Do cool things with math here

    return         


        
相关标签:
1条回答
  • 2021-01-07 03:07

    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
    
    0 讨论(0)
提交回复
热议问题