Skip to content
EN

reearth.viewer

Last updated

The reearth.viewer namespace provides a set of functions to interact with the viewer.

property provides a set of properties about the viewer, including globe, terrain, scene, tiles, sky, and more.

reearth.viewer.property: ViewerProperty;

Type ViewerProperty

Currently, property returns only the properties that have been explicitly set; default values are not included.

The viewport is designed to provide properties related to the map area and includes the URL query parameters for the current viewport (page).

reearth.viewer.viewport: Viewport;

Type

type Viewport = {
width: number;
height: number;
isMobile: boolean;
query: Record<string, string>;
};
  • width: The width of the viewport.
  • height: The height of the viewport.
  • isMobile: A boolean value that indicates whether the viewport is a mobile device based on bowser user-agent detection.
  • query: The URL query parameters for the current page.

env provides the env info of current running Re:Earth Visualizer.

reearth.viewer.env: Env;

Type

type Env = {
inEditor: boolean;
isBuilt: boolean;
};

inEditor and isBuilt has different values when running in different page or tab, plugin could provide different behavior based on these values.

PropertyEditor - Map/Story/Widgets TabEditor - Publish TabPublished Page
inEditortruefalsefalse
isBuiltfalsefalsetrue

interactionMode provides a set of properties and methods to manage the interaction mode of the viewer in Re:Earth Visualizer.

Current interaction mode of the viewer.

reearth.viewer.interactionMode.mode: InteractionModeType

Type InteractionModeType = "default" | "move" | "selection" | "sketch" | "spatialId"

  • default: Default interaction mode.
  • move: Move interaction mode. Selection is disabled in this mode.
  • selection: Selection interaction mode. Move is disabled in this mode.
  • sketch: Sketch interaction mode. Sketch could be enabled in this mode only.
  • spatialId: Spatial ID interaction mode. Use this mode when working with Spatial ID picking.

Overrides the interaction mode of the viewer.

reearth.viewer.interactionMode.override: (
mode: InteractionModeType
) => void;

Type: InteractionModeType

The interaction mode to be set.

None (void). The method performs its operation without returning a value.

overrideProperty is used to override the viewer property.

reearth.viewer.overrideProperty: (property: ViewerProperty) => void;

Type ViewerProperty

Type void

This Method has no return value.

// Enable Terrain
reearth.viewer.overrideProperty({
terrain: {
enabled: true,
},
});

capture function could generate an image for current viewer.

reearth.viewer.capture: (
type?: string,
encoderOptions?: number
) => string | undefined;

Type string (optional)

A string indicating the image format. The default type is image/png; this image format will be also used if the specified type is not supported.

Type number (optional)

A Number between 0 and 1 indicating the image quality to be used when creating images using file formats that support lossy compression (such as image/jpeg or image/webp). A user agent will use its default quality value if this option is not specified, or if the number is outside the allowed range.

Type string | undefined

A string containing the requested data URL.

// Get the capture of current map,
// You can post the returned image string to your widget UI and trigger download.
console.log(reearth.viewer.capture("image/png"));

open method is used to open a URL in a new tab.

reearth.viewer.open: (url: string) => void;

Type string

The URL to be opened in a new tab.

Type void

reload method is used to reload the current Visualizer page.

reearth.viewer.reload: () => void;

Type void

The tools module provides a collection of helper functions for performing various calculations around globe and scene.

Return the location on the earth from the screen coordinate.

reearth.viewer.tools.getLocationFromScreenCoordinate: (
x: number,
y: number,
withTerrain?: boolean
) => { lat: number; lng: number; height: number } | undefined;

Type number

The x pixel coordinate on the viewer.

Type number

The y pixel coordinate on the viewer.

Type boolean (optional)

A boolean value that indicates whether the terrain height should be considered. The default value is false.

Type { lat: number; lng: number; height: number } | undefined

The location on the earth.

Return the screen coordinate from the position on the earth.

reearth.viewer.tools.getScreenCoordinateFromPosition: (
position: [x: number, y: number, z: number]
) => [x: number, y: number] | undefined;

Type [x: number, y: number, z: number]

The position on the earth, in Cartesian.

Type [x: number, y: number] | undefined

The pixel coordinate on viewer.

Return the terrain height at the given location. This is an asynchronous function.

reearth.viewer.tools.getTerrainHeightAsync: (
lng: number,
lat: number
) => Promise<number | undefined>;

Type number

The longitude of the location.

Type number

The latitude of the location.

Type Promise<number | undefined>

The height of the terrain at the given location.

Return the height of the surface at the given location.

reearth.viewer.tools.getGlobeHeight: (
lng: number,
lat: number
) => number | undefined;

Type number

The longitude of the location.

Type number

The latitude of the location.

Type number | undefined

The height of the surface at the given location.

Return the current location of the user. This is an asynchronous function that uses the browser’s Geolocation API.

reearth.viewer.tools.getCurrentLocationAsync: (
options?: Options
) => Promise<Location | undefined>;

Type Options (optional)

An optional object including the following parameters:

  • maximumAge: Maximum age in milliseconds of a cached position. Default value is 0.
  • timeout: Maximum time in milliseconds to wait for a position. Default value is 10,000ms.
  • enableHighAccuracy: Request high accuracy positioning. Default value is false.

Type Promise<Location | undefined>

type Location = {
lat: number;
lng: number;
height: number;
};

The current location of the user.

Converts a cartographic position to a Cartesian position.

reearth.viewer.tools.cartographicToCartesian: (
lng: number,
lat: number,
height: number,
options?: { useGlobeEllipsoid?: boolean }
) => [x: number, y: number, z: number] | undefined;

Type number

The longitude of the location.

Type number

The latitude of the location.

Type number

The height of the location.

Type { useGlobeEllipsoid?: boolean } (optional)

  • useGlobeEllipsoid: A boolean value that indicates whether the globe ellipsoid should be used. The default value is false.

Type [x: number, y: number, z: number] | undefined

The Cartesian position.

Converts a Cartesian position to a cartographic position.

reearth.viewer.tools.cartesianToCartographic: (
x: number,
y: number,
z: number,
options?: { useGlobeEllipsoid?: boolean }
) => [lng: number, lat: number, height: number] | undefined;

Type number

The x coordinate of the location.

Type number

The y coordinate of the location.

Type number

The z coordinate of the location.

Type { useGlobeEllipsoid?: boolean } (optional)

  • useGlobeEllipsoid: A boolean value that indicates whether the globe ellipsoid should be used. The default value is false.

Type [lng: number, lat: number, height: number] | undefined

The cartographic position.

Transforms the position by the offset on the screen.

reearth.viewer.tools.transformByOffsetOnScreen: (
rawPosition: [x: number, y: number, z: number],
screenOffset: [x: number, y: number]
) => [x: number, y: number, z: number] | undefined;

Type [x: number, y: number, z: number]

The raw position on the earth.

Type [x: number, y: number]

The offset on the screen.

Type [x: number, y: number, z: number] | undefined

The transformed position.

Check if the position is visible on the globe.

reearth.viewer.tools.isPositionVisibleOnGlobe: (
position: [x: number, y: number, z: number]
) => boolean;

Type [x: number, y: number, z: number]

The position on the earth.

Type boolean

A boolean value that indicates whether the position is visible on the globe.

resize event will be triggered when the viewer is resized.

reearth.viewer.on("resize", ({width: number, height: number, isMobile:boolean}) => void);
  • width: The width of the viewport.
  • height: The height of the viewport.
  • isMobile: A boolean value that indicates whether the viewport is a mobile device based on bowser user-agent detection.
reearth.viewer.on("resize", ({ width, height, isMobile }) => {
console.log(`width: ${width}, height: ${height}, isMobile: ${isMobile}`);
});

Viewer has a set of mouse events that can be listened to. They have the same parameters.

Supported events are:

  • click
  • doubleClick
  • mouseDown
  • mouseUp
  • rightClick
  • rightDown
  • rightUp
  • middleClick
  • middleDown
  • middleUp
  • mouseMove
  • mouseEnter
  • mouseLeave
  • wheel
reearth.viewer.on("click", (event: MouseEvent)=> void);

event: MouseEvent

type MouseEvent = {
x?: number;
y?: number;
lat?: number;
lng?: number;
height?: number;
layerId?: string;
delta?: number;
};
  • x: the x corrdinate of cursor relative to the viewer.
  • y: the y corrdinate of cursor relative to the viewer.
  • lat: the latitude of cursor on earth.
  • lng: the longitude of cursor on earth.
  • height: the height of cursor on earth.
  • layerId: the layerId of the object that cursor is on.
  • delta: the delta value of wheel event.
reearth.viewer.on("mouseMove", ({ lat, lng, height }) => {
console.log(`lat: ${lat}, lng: ${lng}, height: ${height}`);
});