@@ -79,6 +79,73 @@ var offline = {
7979 } ,
8080} ;
8181
82+ var DEG_TO_RAD = Math . PI / 180.0 ;
83+ var EARTH_RADIUS = 6371000.0 ;
84+
85+ // calculates look angles between two points
86+ // format of a and b should be {lon: 0, lat: 0, alt: 0}
87+ // returns {elevention: 0, azimut: 0, bearing: "", range: 0}
88+ //
89+ // based on earthmath.py
90+ // Copyright 2012 (C) Daniel Richman; GNU GPL 3
91+ function calculate_lookangles ( a , b ) {
92+
93+ // degrees to radii
94+ a . lat = a . lat * DEG_TO_RAD ;
95+ a . lon = a . lon * DEG_TO_RAD ;
96+ b . lat = b . lat * DEG_TO_RAD ;
97+ b . lon = b . lon * DEG_TO_RAD ;
98+
99+ var d_lon = b . lon - a . lon ;
100+ var sa = Math . cos ( b . lat ) * Math . sin ( d_lon ) ;
101+ var sb = ( Math . cos ( a . lat ) * Math . sin ( b . lat ) ) - ( Math . sin ( a . lat ) * Math . cos ( b . lat ) * Math . cos ( d_lon ) ) ;
102+ var bearing = Math . atan2 ( sa , sb ) ;
103+ var aa = Math . sqrt ( Math . pow ( sa , 2 ) + Math . pow ( sb , 2 ) ) ;
104+ var ab = ( Math . sin ( a . lat ) * Math . sin ( b . lat ) ) + ( Math . cos ( a . lat ) * Math . cos ( b . lat ) * Math . cos ( d_lon ) ) ;
105+ var angle_at_centre = Math . atan2 ( aa , ab ) ;
106+ var great_circle_distance = angle_at_centre * EARTH_RADIUS
107+
108+ ta = EARTH_RADIUS + a . alt ;
109+ tb = EARTH_RADIUS + b . alt ;
110+ ea = ( Math . cos ( angle_at_centre ) * tb ) - ta ;
111+ eb = Math . sin ( angle_at_centre ) * tb ;
112+ var elevation = Math . atan2 ( ea , eb ) / DEG_TO_RAD ;
113+
114+ // Use Math.coMath.sine rule to find unknown side.
115+ var distance = Math . sqrt ( Math . pow ( ta , 2 ) + Math . pow ( tb , 2 ) - 2 * tb * ta * Math . cos ( angle_at_centre ) ) ;
116+
117+ // Give a bearing in range 0 <= b < 2pi
118+ bearing += ( bearing < 0 ) ? 2 * Math . PI : 0 ;
119+ bearing /= DEG_TO_RAD ;
120+
121+ var directions = [ 'N' , 'E' , 'S' , 'W' ] ;
122+ var idx = Math . floor ( bearing / 90 ) ;
123+ var str_bearing = "" + directions [ idx ] + " " + Math . round ( bearing % 90 ) + '° ' + directions [ ( idx + 1 ) % 4 ] ;
124+
125+
126+ return {
127+ 'elevation' : elevation ,
128+ 'azimuth' : bearing ,
129+ 'range' : distance ,
130+ 'bearing' : str_bearing
131+ }
132+ }
133+
134+ function update_lookangles ( idx ) {
135+ var a = { lat : GPS_lat , lon : GPS_lon , alt : GPS_alt } ;
136+
137+ var xref = vehicles [ idx ] . curr_position ;
138+ var b = { lat : parseInt ( xref . gps_lat ) , lon : parseInt ( xref . gps_lon ) , alt : parseInt ( xref . gps_alt ) } ;
139+
140+ var look = calculate_lookangles ( a , b ) ;
141+
142+ $ ( "#lookanglesbox .azimuth" ) . text ( "Azimuth: " + Math . round ( look . azimuth * 10000 ) / 10000 + "°" ) ;
143+ $ ( "#lookanglesbox .bearing" ) . text ( look . bearing ) ;
144+ $ ( "#lookanglesbox .elevation" ) . text ( "Elevation: " + Math . round ( look . elevation * 10000 ) / 10000 + "°" ) ;
145+ $ ( "#lookanglesbox .range" ) . text ( Math . round ( look . range / 1000 ) + " km" ) ;
146+
147+ }
148+
82149function load ( ) {
83150 //initialize map object
84151 map = new google . maps . Map ( document . getElementById ( 'map' ) , {
@@ -152,6 +219,10 @@ function unload() {
152219function panTo ( vehicle_index ) {
153220 if ( vehicle_index < 0 ) return ;
154221
222+ // update lookangles
223+ update_lookangles ( vehicle_index ) ;
224+
225+ // pan map
155226 if ( vehicles [ vehicle_index ] . marker_shadow ) map . panTo ( vehicles [ vehicle_index ] . marker_shadow . getPosition ( ) ) ;
156227 else map . panTo ( vehicles [ vehicle_index ] . marker . getPosition ( ) ) ;
157228}
0 commit comments