[GIS] Compute Angle of line in conventional bearing using ArcGIS API for JavaScript

anglesarcgis-javascript-apijavascriptlinepoint

I am trying to compute angle between two lines (line segment) using ESRI (ArcGIS, JS) in conventional bearing (Eg: N29'E)

I am using Add Toolbar to draw a line segment where point A is a fixed point (already drawn) and when the user is hovering over the map looking for point B, it should display projected angle.

Has anybody tried anything like this before or any idea?

Best Answer

For anyone needing this:

function computeAngle(pointA, pointB){
                var dLon = (pointB.x - pointA.x) * Math.PI / 180;
                var lat1 = pointA.y * Math.PI / 180;
                var lat2 = pointB.y * 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 bearing = Math.atan2(y, x)  * 180 / Math.PI;
                bearing = ((bearing + 360) % 360).toFixed(1); //Converting -ve to +ve (0-360)
                if(bearing >= 0 && bearing < 90){
                    return 'N' + (bearing != 0  ? bearing + 'E' : '');
                }
                if(bearing >= 90 && bearing < 180){
                    return (bearing != 90  ? 'S' + (180 - bearing).toFixed(1) : '') + 'E';
                }
                if(bearing >= 180 && bearing < 270){
                    return 'S' + (bearing != 180  ? (bearing - 180).toFixed(1) + 'W' : '');
                }
                if(bearing >= 270){
                    return (bearing != 270  ? 'N' + (360 - bearing).toFixed(1) : '') + 'W';
                }
                return 'N';
}

References:

http://www.movable-type.co.uk/scripts/latlong.html

http://www.mathsteacher.com.au/year7/ch08_angles/07_bear/bearing.htm

Related Question