Map
The main class for map initialization.
(container, options)
Example:
const map = new mapgl.Map('map', {
center: [55.185346, 25.14226],
zoom: 16,
});
container
HTMLElementstring
The HTML element in which the map appears, or its ID.
options
MapOptions
Map initialization options.
(name, config)
Adds a config of user icon that will be used in the current style. If an icon with the same name already exists, it won't be added and applied to the current style.
name
string
A user icon name.
config
StyleIconConfig
A config with parameters for user icon.
this
(layer, beforeId?)
Adds a style layer to the current map style.
layer
Layer
Style layer object.
beforeId?
undefinedstring
The identifier of an existing layer to insert the new layer before, resulting in the new layer appearing visually beneath the existing layer. If this argument is not specified, the layer will be appended to the end of the layers array and appear visually above all other layers.
Throws an error if:
- the layer with specified id already exists in the current map style;
- the layer with specified beforeId doesn't exist in the current map style;
- one group layer is inserted into another group layer.
this
()
Destroys the map and frees all related resources.
void
(type, data?)
Calls all event listeners with event type type
type
K
Event type
data?
MapEventTable[K]
Data transferred to events
this
(bounds, options?)
Pans and zooms the map to contain its visible area within the specified geographical bounds. This method also resets the map pitch and rotation to 0. But the map rotation can be saved by option considerRotation
bounds
LngLatBounds
The geographical bounds to fit in
options?
FitBoundsOptions
FitBounds options
this
(options?)
Returns the geographical bounds visible in the current map view.
()
Returns HTMLCanvasElement of the map.
HTMLCanvasElement
()
Returns the current map center in geographical coordinates ([longitude, latitude]
).
number[]
()
Returns HTML-container of the map.
HTMLElement
()
Returns the desired map language.
string
()
Returns the current maximum map zoom.
number
()
Returns the current minimum map zoom.
number
(option)
Gets a value of the map option.
option
SupportedSimpleOpts
Map option name. Only 'disableDragging', 'enableTrackResize' options are available
undefinedfalsetrue
()
Returns the current map pitch in degrees.
number
()
Returns the view projection matrix associated with the current state of the map.
number[]
()
Returns the current map rotation in degrees.
number
()
Returns the current map size in logical pixels.
number[]
()
Returns the current map style variables.
Available global variables: _activeFloorBuildingIds — an array of building IDs with active floor plans _activeFloorIds — an array of active floor IDs from buildings with active floor plans _activeFloorIsMetro - a flag which tells if active floors plan is a metro schema trafficOn - a flag which tells if traffic is enabled parkingOn - a flag which tells if parking is enabled (used in native version of 2gis) navigatorOn - a flag which tells if navigator is enabled (used in native version of 2gis) immersiveRoadsOn - a flag which tells if immersive roads are enabled terrainEnabled - a flag which tells if 3d terrain is enabled
()
Returns the current map style zoom.
number
()
Returns the current map zoom.
number
(layerId)
Checks if a style layer with the given identifier exists in the current map style.
layerId
string
The identifier of a layer.
boolean
(params)
Hides the layer from the current map style and updates the active layers. If only the specified id, then a certain layer will be hidden by id. If only the specified type, then all layers with this type will be hidden. If both the layer type and id are specified, layers will be hidden by type, and the id will be ignored.
params
this
()
Hides the traffic layer from the map.
this
()
Updates the map size. This method should be called after any change in the map container size.
this
()
Returns true if the map isn't moving and has rendered all the viewport tiles.
boolean
(type, listener)
Removes event listener registered with on
type
K
Event type
listener
(ev: MapEventTable[K]) => void
Event handler
this
(type, listener)
Registers event listener
type
K
Event type
listener
(ev: MapEventTable[K]) => void
Event handler
this
(type, listener)
Registers event listener which will be called once
type
K
Event type
listener
(ev: MapEventTable[K]) => void
Event handler
this
(styleState)
Patches map style global variables. Use this method if you want to change a particular variable and left other ones intact.
(lngLat)
Returns pixel coordinates [x, y, z]
, relative to the map's container, that correspond to the specified geographical coordinates.
Z
coordinate is deprecated.
lngLat
number[]
The geographical coordinates [longitude, latitude]
to project.
number[]
(name)
Removes a specified icon from the current style.
name
string
An icon name to remove.
this
(layerId)
Removes the layer with the given identifier from the current map style.
layerId
string
The identifier of the layer to remove.
Throws an error if layer with specified id does not exist in the current map style.
this
(center, options?)
Sets the geographical center of the map.
center
number[]
The desired coordinates ([longitude, latitude]
).
options?
AnimationOptions
Center animation options.
this
(padding)
Sets padding for controls layout.
padding
Partial<Padding>
void
(floorPlanId, floorLevelIndex)
Sets a floorLevel of the floorPlan.
floorPlanId
string
id of the floor plan
floorLevelIndex
number
floorLevelIndex in floorLevels list
void
(lang)
Sets the desired map language.
lang
string
short string code 'en', 'ru', ...etc
this
(pitch, options?)
Sets the maximum map pitch for low zooms (zoom < 16.5).
pitch
number
The desired maximum pitch in degrees.
options?
AnimationOptions
Options for animating the pitch in case the current pitch is bigger than the new maximum.
this
(bounds)
Sets a bound to limit movings on map
(pitch, options?)
Sets the maximum map pitch.
pitch
number
The desired maximum pitch in degrees.
options?
AnimationOptions
Options for animating the pitch in case the current pitch is more than the new maximum.
this
(zoom, options?)
Sets the maximum map zoom.
zoom
number
The desired maximum zoom.
options?
AnimationOptions
Options for adjusting map zoom in case the current zoom is more than the new maximum.
this
(pitch, options?)
Sets the minimum map pitch.
pitch
number
The desired minimum pitch in degrees.
options?
AnimationOptions
Options for animating the pitch in case the current pitch is less than the new minimum.
this
(zoom, options?)
Sets the minimum map zoom.
zoom
number
The desired minimum zoom.
options?
AnimationOptions
Options for adjusting map zoom in case the current zoom is less than the new minimum.
this
(option, value)
Sets a new value for the map option.
option
T
Map option name. Only 'disableDragging', 'enableTrackResize', 'loopWorld' options are available
value
NonNullable<MapOptions[T]>
New value of the map option.
this
(padding, options?)
Sets the map padding. The padding on each side has a clamp to a positive value no larger than the map canvas size for either side
padding
Partial<Padding>
Padding in pixels from the different sides of the map canvas
options?
AnimationOptions
Padding animation options.
this
(pitch, options?)
Sets the map pitch angle.
pitch
number
The desired map pitch in degrees.
options?
AnimationOptions
Pitch animation options.
this
(rotation, options?)
Sets the map rotation angle.
rotation
number
The desired map rotation in degrees.
options?
RotationAnimationOptions
Rotation animation options.
this
(ids, scope?)
Selects objects on the map by identifiers.
ids
string[]
An array of identifiers of objects that should be selected.
scope?
undefinedstring
The scope of the identifiers. If not specified default scope is used.
this
(styleId)
Uploads styles object by its id and applies it to the map. Returns a promise that contains param styleId.
styleId
string
uuid of the style.
Promise<string>
(styleUrl, options)
Experimental method. Uploads styles object from URL and applies it to the map. Returns a promise that contains param styleId.
Warning: Setting a style this way might result in an incompatible style being applied. Map styles are constantly evolving in order to display new cartographic data. By storing the style on your own side, you assume the responsibility to update it periodically and take care of its relevance.
Use it only if you know that the style at this url is compatible with your version of mapgl. In other cases it is recommended to use setStyleById to set a style from the Style Editor.
styleUrl
string
Base URL of the style. It is used to resolve paths provided in the options.
options
StyleOptions
Contains paths to styles, icons, fonts, models etc
Promise<string>
(styleState)
Sets whole map style global variables at once, any previously set variables will be reset or overridden.
(styleZoom, options?)
Sets the map style zoom.
(zoom, options?)
Sets the map zoom.
(params)
Shows the layer from the current map style and updates the active layers. If only the specified id, then only a certain layer will be visible. If only the specified type, then all layers with this type will be visible. If both the layer type and id are specified, layers will be visible by type, and the id will be ignored.
params
this
()
Shows the traffic layer on the map.
this
()
Calls the map rerender.
void
(point)
Returns geographical coordinates [longitude, latitude]
that correspond to the specified pixel coordinates.
point
number[]
The pixel coordinates [x, y]
of the map's container to unproject.
number[]