Earth API

Setup an Earth

new Earth( element, options ) returns EarthInstance

element
HTMLElement or ID (String) of the container element
options
Object of properties
<div id="myearth" class="earth-container"></div>
var myearth = new Earth( "myearth", {
	location: { lat : 22.5, lng : 20 },
	mapLandColor: '#333333',
	mapSeaColor: '#DDDDDD'
} );

Minimal Example
Asynchronous Integration
Preload & Fallback

You can access the EarthInstance by the earth property of the container element: console.log( document.getElementById('myearth').earth.location );
Wait for the ready event before you change properties or add objects.

Properties

location (lat/lng object)

{ lat: 0, lng: 0 }

The geo location at the center of the earth. Also see the goTo method.

polarLimit (float)

0.3

0.0 - 1.0

Constrains the earth's rotation when far northern and southern locations are shown to shift the focus to the Equator.
0 = no limits, 1 = fully constrained, the Equator is always at the center
Also see the dragPolarLimit property.
Example: Polar Limit

mapLandColor (color) * default map

#F4F4F4

mapSeaColor (color) * default map

#0099FF

mapBorderColor (color) * default map

value of mapLandColor

mapBorderWidth (float) * default map

0.3

0.25 - 1.0

In order to avoid display errors, you should not use values smaller than 0.25. If you want no borders, set mapBorderColor to the value of mapLandColor.

mapStyles (css string) * default map

This CSS string is applied to the default SVG map. You can target countries by their ISO code.

mapStyles : "#US { fill: blue; stroke: blue; }"

Example: Map Styles > Colored Countries
Example: Country Selector

mapImage (URL String) *

"" (uses the default SVG map)

You can use a SVG or any pixel image format. The image must be in 2:1 format, with equirectangular projection. SVGs must be saved with width and height attributes (not "responsive" mode Illustrator).
Example: Map Image
Example: Custom SVG Map

 
* If you need to change the map* properties after initialization call the redrawMap() method to apply the new values.

transparent (boolean)

false

The earth surface becomes transparent if set to true and mapSeaColor has a value like RGBA(0, 0, 255, 0.5) or you use a mapImage with transparency.
Example: Earth Transparency

innerOpacity (float)

1.0

0.0 - 1.0

Controls the opacity of the inside of the earth.

innerColor (color)

#FFFFFF

Multiplys a color on the inside of the earth.

draggable (boolean)

true

The following properties are only available if draggable: true.

grabCursor (boolean)

true

dragMomentum (boolean)

true

If true the earth will keep spinning a while after dragging/swiping.

dragDamping (float)

0.7

0.01 - 0.99

Controls how fast the earth loses momentum after dragging/swiping.

dragPolarLimit (float)

0.3

0.0 - 1.0

Like the polarLimit property but constrains the earth's rotation during dragging/swiping.

zoom (float)

1

Also see the zoom option of the goTo method.

zoomable (boolean)

false

Zoomable by mouse wheel and gestures.

zoomMin (float)

0.5

zoomMax (float)

1.25

zoomSpeed (float)

1

Zoom speed when zooming by mouse wheel or gestures.

autoRotate (boolean)

false

The following properties are only available if autoRotate: true.

autoRotateSpeed (float)

1

A negative value reverses the rotation direction.

autoRotateSpeedUp (float)

0

Like autoRotateSpeed property but for up/down rotation.

autoRotateDelay (float)

1000 (ms)

Time to wait before auto rotating starts.

autoRotateStart (float)

1000 (ms)

Acceleration time when auto rotating starts.

autoRotateEasing (easing)

in-quad

Acceleration easing when auto rotating starts.

light ("none", "simple" or "sun")

"simple"

Example (Light Types)

lightAmbience (float)

if light: none 1 else 0.5

Controls the overall brightness.

lightIntensity (float)

0.5

Intensity of the sun or simple light.

lightColor (color)

#FFFFFF

lightGroundColor (color)

#999999

Only available if light: simple.

sunDirection (x/y object) or false

false

Only available if light: sun. Provide a x/y object with values from -1 to 1.
E.g. { x: 0, y: -0.8 } for light from above.

sunLocation (lat/lng object)

{ lat: 0, lng: 0 }

Only available if light: sun and sunDirection: false. The sun shines on the given location.

shadows (boolean)

true

Only available if light: sun.

Advanced:

mapHitTest (boolean)

false

Only available for the default SVG map or a custom SVG map set as mapImage.
Set to true to determine the ID of the hit SVG path (country code) during click, mousedown and mouseup events.
Example: Get Location

showHotspots (boolean)

false

Set to true to display wireframes of the marker hotspots for debugging.

quality (integer)

3 or 4 depending on container size

1 - 6

Controls various internal parameters like texture resolution and polygon count.
1 = very low quality, 2 = low quality, 3 = good quality (up to 1000px), 4 = high quality, ...

shininess (float)

0.1

0.0 - 1.0

Only if light: sun. Shininess of the earth surface.

legacySupportIE11 (boolean)

false

Set to true if you need to support IE11.

fallbackMapUrlIE11 (URL string)

false

If you use legacySupportIE11: true, you can specify a map image that is only used by IE11.
Don't use this property if you use mapImage anyway.

mapCanvas (CanvasElement)

Reference to the canvas of the map texture.

mapContext (CanvasRenderingContext2D)

Reference to the rendering context of the map texture. These properties are available during and after the drawtexture event.
Call updateMap() after drawing on the mapCanvas to update the earth's texture.

About CanvasRenderingContext2D
Example: Draw Map Locations

canvas (CanvasElement)

The earth is rendered to this canvas element.

preserveDrawingBuffer (boolean)

false

Set to true if you want to retrieve image data of the canvas.
Example: Capture Canvas Image

paused (boolean)

false

You can pause the earth to save performance when hiding the earth. The earth is not updated.

deltaTime (float)

Milliseconds since the last animation frame.

scene, camera, renderer, sphere (THREE)

You can access the THREE.js objects that render the earth. About THREE.js

console.log( myearth.scene );

Methods

addMarker( options ) returns MarkerInstance

options
Object of marker properties

addImage( options ) returns ImageInstance

options
Object of image properties

addSprite( options ) returns SpriteInstance

options
Object of sprite properties

addPoints( options ) returns PointsInstance

options
Object of points properties

addLine( options ) returns LineInstance

options
Object of line properties

addOverlay( options ) returns OverlayInstance

options
Object of overlay properties

goTo( location, options ) returns AnimationInstance or false if navigation is not needed

location
lat/lng object
options
Object of animation properties, approachAngle and zoom

You can animate the earth's location property for navigation, but using the goTo() method has advantages:
- previous animations are stopped
- you can use the special option approachAngle to navigate only if needed and only as much as needed.
- you can use the special option zoom to start a synchronosized zoom animation

myearth.goTo( { lat: -25, lng: 131 }, { relativeDuration: 100, approachAngle: 20, zoom: 1.1 } );

Example: Earth Navigation

Advanced:

redrawMap( )

Call redrawMap() if you need to change the map*Color/mapStyles/mapImage properties after initialization.
Example: Country Selector
Example: Map Image

updateMap( )

Call updateMap() after drawing on the mapCanvas to update the earth's texture.

hitTest( location )

location
(lat/lng object) Location on the map (mapSvg)

Only available for the default SVG map or a custom SVG map set as mapImage.
Returns the ID of the SVG path element found at location.
If you use the default SVG map the ID equals the country ISO code.
Example: getLocation / hitTest

startAutoRotate( easeIn )

easeIn
(boolean) default: false

Skips the autoRotateDelay. If easeIn is false the earth immediatly rotates at full speed.

resetAutoRotate( )

Interrupts auto rotating. The auto rotation restarts after the autoRotateDelay.

getPoint( location, offset ) returns x/y object

location
lat/lng object
offset
distance to the earth surface (number) default: 0

Get a x/y point of lat/lng location relative to the container element.
Example: getPoint

getLocation( point ) returns lat/lng object or false if the point is not on the earth

point
mouse or touch position (x/y object) relative to the page

Get a lat/lng object of a x/y point on the page.
Example: getLocation / hitTest

getRadius( ) returns number

Get the current radius of the earth in pixels.
Example: getRadius

destroy( )

Removes the earth and frees memory.

Static Methods

Earth.addMesh( objString )

objString
A .obj 3d file as a string

You can add own 3d meshes for markers. Learn more
Example: Custom Marker Meshes

Earth.isSupported( legacySupportIE11 ) returns true/false

legacySupportIE11
boolean (default: false)

Returns true if WebGL is supported. For Internet Explorer 11 this method only returns true if legacySupportIE11 is true.
Example: Legacy Support (IE 11)

Advanced:

Earth.getDistance( from, to ) returns number

from
lat/lng object
to
lat/lng object

Returns the approximate distance between two locations in kilometers.
Multiply by 0.621371 to get the distance in miles.

Earth.getAngle( from, to ) returns number

from
lat/lng object
to
lat/lng object

Returns the approximate angle between two locations in degrees (0.0 - 180.0).

Earth.lerp( from, to, time, lerpLatLng ) returns lat/lng object

from
lat/lng object
to
lat/lng object
time
number 0.0 - 1.0
lerpLatLng
boolean

Returns a lat/lng location between from and to at time. If lerpLatLng is true the lat/lng values are lerped as numbers (avoiding the polar regions) instead of the 3d position (shortest path).

Example: Distance, Angle and Lerp

Events

ready

The ready event is triggered once when the earth is initialized and you can start adding objects.

drawtexture

The drawtexture event is triggered on initialization or when the redrawMap() method is called. You can use this event to draw on the mapCanvas to modify the earth's texture.
Example: Draw Map Locations

click, mousedown or mouseup

event.x / event.y
Position relative to the top left corner of the container element
event.location
Location as lat/lng object
event.id
Only available if mapHitTest is true. The ID of the hit SVG path (country code).

Example: Get Location

myearth.addEventListener( 'click', function( event ) {
	console.log( event.location );
} );
touchstart/touchend events are triggered as mousedown/mouseup

dragstart, dragend

The events are triggered when the earth is dragged.

change

The change event is triggered when the earth rotates or zooms.

update

The update event is triggered permanently, each animation frame.


You can add native event listeners to the container or canvas element.
The window element triggers the earthjsload event when the library is loaded.

CSS Classes

earth-container on container element

The earth-container class is added to the container element if it is not present. You can set this class in your container elements HTML so that the earth consumes space even before the earth is initialized.

earth-ready on container element

As soon as the earth is ready, the class earth-ready is added.

earth-show-fallback on container element

earth-show-fallback is added if JavaScript is enabled but the earth is not supported. See Earth.isSupported.

earth-fallback on child elements of the container

Child elements with the class earth-fallback are set to hidden if the earth is supported.

earth-clickable on container element

earth-clickable is added while hovering over a clickable hotspot.

earth-draggable on container element

earth-draggable is added while hovering over a draggable earth.

earth-dragging on <html> element

earth-dragging is added to the html element as long as an earth is dragged.

 
To see what these classes do by default you can have a look at the file src/earth.css