1- /* Nite v1.4
1+ /* Nite v1.5
22 * A tiny library to create a night overlay over the map
33 * Author: Rossen Georgiev @ https://github.com/rossengeorgiev
44 * Requires: GMaps API 3
88var nite = {
99 map : null ,
1010 date : null ,
11- marker_sun : null ,
12- marker_shadow : null ,
13- marker_shadow_lite : null ,
14- shadow_radius : parseInt ( 6371 * Math . PI * 500 ) ,
1511 sun_position : null ,
12+ earth_radius_meters : 6371008 ,
13+ marker_twilight_civil : null ,
14+ marker_twilight_nautical : null ,
15+ marker_twilight_astronomical : null ,
16+ marker_night : null ,
1617
1718 init : function ( map ) {
1819 if ( typeof google === 'undefined'
@@ -21,27 +22,51 @@ var nite = {
2122 this . map = map ;
2223 this . sun_position = this . calculatePositionOfSun ( ) ;
2324
24- this . marker_shadow = new google . maps . Circle ( {
25+ this . marker_twilight_civil = new google . maps . Circle ( {
2526 map : this . map ,
2627 center : this . getShadowPosition ( ) ,
27- radius : this . shadow_radius ,
28+ radius : this . getShadowRadiusFromAngle ( 0 ) ,
2829 fillColor : "#000" ,
2930 fillOpacity : 0.1 ,
3031 strokeOpacity : 0 ,
3132 clickable : false ,
3233 editable : false
3334 } ) ;
34-
35- this . marker_shadow_lite = new google . maps . Circle ( {
35+ this . marker_twilight_nautical = new google . maps . Circle ( {
36+ map : this . map ,
37+ center : this . getShadowPosition ( ) ,
38+ radius : this . getShadowRadiusFromAngle ( 6 ) ,
39+ fillColor : "#000" ,
40+ fillOpacity : 0.1 ,
41+ strokeOpacity : 0 ,
42+ clickable : false ,
43+ editable : false
44+ } ) ;
45+ this . marker_twilight_astronomical = new google . maps . Circle ( {
3646 map : this . map ,
3747 center : this . getShadowPosition ( ) ,
38- radius : this . shadow_radius * 0.96 ,
48+ radius : this . getShadowRadiusFromAngle ( 12 ) ,
3949 fillColor : "#000" ,
4050 fillOpacity : 0.1 ,
4151 strokeOpacity : 0 ,
4252 clickable : false ,
4353 editable : false
4454 } ) ;
55+ this . marker_night = new google . maps . Circle ( {
56+ map : this . map ,
57+ center : this . getShadowPosition ( ) ,
58+ radius : this . getShadowRadiusFromAngle ( 18 ) ,
59+ fillColor : "#000" ,
60+ fillOpacity : 0.1 ,
61+ strokeOpacity : 0 ,
62+ clickable : false ,
63+ editable : false
64+ } ) ;
65+ } ,
66+ getShadowRadiusFromAngle : function ( angle ) {
67+ var shadow_radius = this . earth_radius_meters * Math . PI * 0.5 ;
68+ var twilight_dist = ( ( this . earth_radius_meters * 2 * Math . PI ) / 360 ) * angle ;
69+ return shadow_radius - twilight_dist ;
4570 } ,
4671 getSunPosition : function ( ) {
4772 return this . sun_position ;
@@ -52,8 +77,11 @@ var nite = {
5277 refresh : function ( ) {
5378 if ( ! this . isVisible ( ) ) return ;
5479 this . sun_position = this . calculatePositionOfSun ( this . date ) ;
55- this . marker_shadow . setCenter ( this . getShadowPosition ( ) ) ;
56- this . marker_shadow_lite . setCenter ( this . getShadowPosition ( ) ) ;
80+ var shadow_position = this . getShadowPosition ( ) ;
81+ this . marker_twilight_civil . setCenter ( shadow_position ) ;
82+ this . marker_twilight_nautical . setCenter ( shadow_position ) ;
83+ this . marker_twilight_astronomical . setCenter ( shadow_position ) ;
84+ this . marker_night . setCenter ( shadow_position ) ;
5785 } ,
5886 jday : function ( date ) {
5987 return ( date . getTime ( ) / 86400000.0 ) + 2440587.5 ;
@@ -90,19 +118,25 @@ var nite = {
90118 } ,
91119 setMap : function ( map ) {
92120 this . map = map ;
93- this . marker_shadow . setMap ( this . map ) ;
94- this . marker_shadow_lite . setMap ( this . map ) ;
121+ this . marker_twilight_civil . setMap ( this . map ) ;
122+ this . marker_twilight_nautical . setMap ( this . map ) ;
123+ this . marker_twilight_astronomical . setMap ( this . map ) ;
124+ this . marker_night . setMap ( this . map ) ;
95125 } ,
96126 show : function ( ) {
97- this . marker_shadow . setVisible ( true ) ;
98- this . marker_shadow_lite . setVisible ( true ) ;
127+ this . marker_twilight_civil . setVisible ( true ) ;
128+ this . marker_twilight_nautical . setVisible ( true ) ;
129+ this . marker_twilight_astronomical . setVisible ( true ) ;
130+ this . marker_night . setVisible ( true ) ;
99131 this . refresh ( ) ;
100132 } ,
101133 hide : function ( ) {
102- this . marker_shadow . setVisible ( false ) ;
103- this . marker_shadow_lite . setVisible ( false ) ;
134+ this . marker_twilight_civil . setVisible ( false ) ;
135+ this . marker_twilight_nautical . setVisible ( false ) ;
136+ this . marker_twilight_astronomical . setVisible ( false ) ;
137+ this . marker_night . setVisible ( false ) ;
104138 } ,
105139 isVisible : function ( ) {
106- return this . marker_shadow . getVisible ( ) ;
140+ return this . marker_night . getVisible ( ) ;
107141 }
108142}
0 commit comments