Night and Day on Earth

Leaflet.Terminator is a simple plug-in to the Leaflet library to overlay day and night regions on maps. You can get the source code on GitHub. The example below shows a continously updating world map.

To use Leaflet.Terminator, include

	
      
in your html. Alternatively, if you use npm, Leaflet.Terminator is available as @joergdietrich/leaflet.terminator. Generate your Leaflet map as usual and then:
        L.terminator().addTo(map);
      

The L.terminator() method returns a Polygon object, which can be manipulated in the usual ways. For example, the auto-updating map above uses the following code to redraw the terminator every 0.5 seconds:

	  var t = L.terminator();
	  t.addTo(map);
	  setInterval(function(){updateTerminator(t)}, 500);
	  function updateTerminator(t) {
	    var t2 = L.terminator();
	    t.setLatLngs(t2.getLatLngs());
	    t.redraw();
	  }

You can zoom in and watch the terminator move. However, the equations used by Leaflet.Terminator are accurate only to a few seconds to minimize computing time. More importantly, they ignore refraction, which changes the actual sun rise and set times by several minutes. Also keep in mind that the exact times depend on terrain – your altitude and your view to the horizon.

Leaflet.Terminator computes the polygon from longitudes -360° to +360°, i.e., twice around the Earth to allow for some panning in world maps. Nevertheless, it is probably a good idea to either use the worldCopyJump or maxBounds options when instantiating the map.

Leaflet.Terminator extends the Polygon class and thus L.terminator() takes all Polyline options. In addition L.terminator() understands the following options:

Option Type Default Description
resolution Number 2 Set the separation at which Polygon points are computed. Polygon points are located at fixed longitude points separated by 1°/resolution.
time value

dateString

year, month, day [, hour, minute, second, millisecond]
null Set the time for which the terminator is computed. The default is the present time. Any expression understood by Date() and which evaluates to true can be used.

The map below shows the terminator across northern Europe shortly before summer solstice generated by calling

      L.terminator({time: "2013-06-20T21:00:00Z"});
      

Leaflet.Terminator modifies the default Path options as follows:

Option Value
color '#00'
fillColor '#00'
fillOpacity 0.5