1- /* Nite v1.0
1+ /* Nite v1.2
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
@@ -14,13 +14,13 @@ var nite = {
1414 shadow_radius : parseInt ( 6371 * Math . PI * 500 ) ,
1515 sun_position : null ,
1616
17- init : function ( map ) {
17+ init : function ( map ) {
1818 if ( typeof google === 'undefined'
1919 || typeof google . maps === 'undefined' ) throw "Nite Overlay: no google.maps detected" ;
20-
20+
2121 this . map = map ;
22- this . refreshSunZenith ( ) ;
23-
22+ this . sun_position = this . calculatePositionOfSun ( ) ;
23+
2424 this . marker_shadow = new google . maps . Circle ( {
2525 map : this . map ,
2626 center : this . getShadowPosition ( ) ,
@@ -29,8 +29,8 @@ var nite = {
2929 fillOpacity : 0.1 ,
3030 strokeOpacity : 0 ,
3131 clickable : false ,
32- editable : false
33- } ) ;
32+ editable : false
33+ } ) ;
3434
3535 this . marker_shadow_lite = new google . maps . Circle ( {
3636 map : this . map ,
@@ -40,36 +40,40 @@ var nite = {
4040 fillOpacity : 0.1 ,
4141 strokeOpacity : 0 ,
4242 clickable : false ,
43- editable : false
44- } ) ;
43+ editable : false
44+ } ) ;
45+ } ,
46+ getSunPosition : function ( ) {
47+ return this . sun_position ;
4548 } ,
4649 getShadowPosition : function ( ) {
47- return new google . maps . LatLng ( - this . sun_position . lat ( ) , - this . sun_position . lng ( ) + 180 ) ;
50+ return ( this . sun_position ) ? new google . maps . LatLng ( - this . sun_position . lat ( ) , - this . sun_position . lng ( ) + 180 ) : null ;
4851 } ,
4952 refresh : function ( ) {
5053 if ( ! this . isVisible ( ) ) return ;
51- this . refreshSunZenith ( ) ;
54+ this . sun_position = this . calculatePositionOfSun ( this . date ) ;
5255 this . marker_shadow . setCenter ( this . getShadowPosition ( ) ) ;
5356 this . marker_shadow_lite . setCenter ( this . getShadowPosition ( ) ) ;
5457 } ,
55- refreshSunZenith : function ( ) {
58+ jday : function ( date ) {
59+ return ( date . getTime ( ) / 86400000.0 ) + 2440587.5 ;
60+ } ,
61+ calculatePositionOfSun : function ( date ) {
62+ date = ( date instanceof Date ) ? date : new Date ( ) ;
63+
5664 var rad = 0.017453292519943295 ;
5765
5866 // based on NOAA solar calculations
59- var utc = new Date ( ) ;
60- if ( this . date ) { utc = new Date ( this . date ) ; }
61- utc = new Date ( utc . getTime ( ) + utc . getTimezoneOffset ( ) * 60000 ) ;
62- var mins_past_midnight = ( utc . getHours ( ) * 60 + utc . getMinutes ( ) ) / 1440 ;
63- var jd = 2415018.5 - ( ( new Date ( "01/01/1900 00:00:00 UTC" ) ) . getTime ( ) - utc . getTime ( ) ) / 1000 / 60 / 60 / 24 ;
64- var jc = ( jd - 2451545 ) / 36525 ;
67+ var mins_past_midnight = ( date . getUTCHours ( ) * 60 + date . getUTCMinutes ( ) ) / 1440 ;
68+ var jc = ( this . jday ( date ) - 2451545 ) / 36525 ;
6569 var mean_long_sun = ( 280.46646 + jc * ( 36000.76983 + jc * 0.0003032 ) ) % 360 ;
6670 var mean_anom_sun = 357.52911 + jc * ( 35999.05029 - 0.0001537 * jc ) ;
6771 var sun_eq = Math . sin ( rad * mean_anom_sun ) * ( 1.914602 - jc * ( 0.004817 + 0.000014 * jc ) ) + Math . sin ( rad * 2 * mean_anom_sun ) * ( 0.019993 - 0.000101 * jc ) + Math . sin ( rad * 3 * mean_anom_sun ) * 0.000289 ;
6872 var sun_true_long = mean_long_sun + sun_eq ;
6973 var sun_app_long = sun_true_long - 0.00569 - 0.00478 * Math . sin ( rad * 125.04 - 1934.136 * jc ) ;
7074 var mean_obliq_ecliptic = 23 + ( 26 + ( ( 21.448 - jc * ( 46.815 + jc * ( 0.00059 - jc * 0.001813 ) ) ) ) / 60 ) / 60 ;
7175 var obliq_corr = mean_obliq_ecliptic + 0.00256 * Math . cos ( rad * 125.04 - 1934.136 * jc ) ;
72-
76+
7377 var lat = Math . asin ( Math . sin ( rad * obliq_corr ) * Math . sin ( rad * sun_app_long ) ) / rad ;
7478
7579 var eccent = 0.016708634 - jc * ( 0.000042037 + 0.0000001267 * jc ) ;
@@ -78,25 +82,25 @@ var nite = {
7882 var true_solar_time = ( mins_past_midnight * 1440 + rq_of_time ) % 1440 ;
7983 var lng = ( true_solar_time / 4 < 0 ) ? true_solar_time / 4 + 180 : true_solar_time / 4 - 180 ;
8084
81- this . sun_position = new google . maps . LatLng ( lat , lng ) ;
85+ return new google . maps . LatLng ( lat , lng ) ;
8286 } ,
8387 setDate : function ( date ) {
84- this . date = date ;
88+ this . date = date ;
8589 this . refresh ( ) ;
8690 } ,
8791 setMap : function ( map ) {
88- this . map = map ;
92+ this . map = map ;
8993 } ,
9094 show : function ( ) {
91- this . marker_shadow . setVisible ( true ) ;
92- this . marker_shadow_lite . setVisible ( true ) ;
95+ this . marker_shadow . setVisible ( true ) ;
96+ this . marker_shadow_lite . setVisible ( true ) ;
9397 this . refresh ( ) ;
9498 } ,
9599 hide : function ( ) {
96- this . marker_shadow . setVisible ( false ) ;
97- this . marker_shadow_lite . setVisible ( false ) ;
100+ this . marker_shadow . setVisible ( false ) ;
101+ this . marker_shadow_lite . setVisible ( false ) ;
98102 } ,
99103 isVisible : function ( ) {
100- return this . marker_shadow . getVisible ( ) ;
104+ return this . marker_shadow . getVisible ( ) ;
101105 }
102106}
0 commit comments