The Map object represents the map on your page. It exposes methods
and properties that enable you to programmatically change the map,
and fires events as users interact with it.
You create a Map by specifying a container and other options.
Then Mapbox GL JS initializes the map on the page and returns your Map
object.
Extends Evented.
Parameters
| Name |
Description |
options.container (HTMLElement | string)
|
The HTML element in which Mapbox GL JS will render the map, or the element's string
id
.
|
options.minZoom [number]
(default 0)
|
The minimum zoom level of the map (1-20).
|
options.maxZoom [number]
(default 20)
|
The maximum zoom level of the map (1-20).
|
options.style [(Object | string)]
|
The map's Mapbox style. This must be an a JSON object conforming to
the schema described in the
Mapbox Style Specification
, or a URL to
such JSON.
To load a style from the Mapbox API, you can use a URL of the form mapbox://styles/:owner/:style,
where :owner is your Mapbox account name and :style is the style ID. Or you can use one of the following
the predefined Mapbox styles:
mapbox://styles/mapbox/streets-v9
mapbox://styles/mapbox/outdoors-v9
mapbox://styles/mapbox/light-v9
mapbox://styles/mapbox/dark-v9
mapbox://styles/mapbox/satellite-v9
mapbox://styles/mapbox/satellite-streets-v9
Tilesets hosted with Mapbox can be style-optimized if you append ?optimize=true to the end of your style URL, like mapbox://styles/mapbox/streets-v9?optimize=true.
Learn more about style-optimized vector tiles in our API documentation.
|
options.hash [boolean]
(default false)
|
If
true
, the map's position (zoom, center latitude, center longitude, bearing, and pitch) will be synced with the hash fragment of the page's URL.
For example,
http://path/to/my/page.html#2.59/39.26/53.07/-24.1/60
.
|
options.interactive [boolean]
(default true)
|
If
false
, no mouse, touch, or keyboard listeners will be attached to the map, so it will not respond to interaction.
|
options.bearingSnap [number]
(default 7)
|
The threshold, measured in degrees, that determines when the map's
bearing (rotation) will snap to north. For example, with a
bearingSnap
of 7, if the user rotates
the map within 7 degrees of north, the map will automatically snap to exact north.
|
options.classes [Array<string>]
|
Mapbox style class names with which to initialize the map.
Keep in mind that these classes are used for controlling a style layer's paint properties, so are
not
reflected
in an HTML element's
class
attribute. To learn more about Mapbox style classes, read about
Layers
in the style specification.
|
options.attributionControl [boolean]
(default true)
|
If
true
, an
AttributionControl
will be added to the map.
|
options.failIfMajorPerformanceCaveat [boolean]
(default false)
|
If
true
, map creation will fail if the performance of Mapbox
GL JS would be dramatically worse than expected (i.e. a software renderer would be used).
|
options.preserveDrawingBuffer [boolean]
(default false)
|
If
true
, the map's canvas can be exported to a PNG using
map.getCanvas().toDataURL()
. This is
false
by default as a performance optimization.
|
options.maxBounds [LngLatBoundsLike]
|
If set, the map will be constrained to the given bounds.
|
options.scrollZoom [boolean]
(default true)
|
If
true
, the "scroll to zoom" interaction is enabled (see
ScrollZoomHandler
).
|
options.boxZoom [boolean]
(default true)
|
If
true
, the "box zoom" interaction is enabled (see
BoxZoomHandler
).
|
options.dragRotate [boolean]
(default true)
|
If
true
, the "drag to rotate" interaction is enabled (see
DragRotateHandler
).
|
options.dragPan [boolean]
(default true)
|
If
true
, the "drag to pan" interaction is enabled (see
DragPanHandler
).
|
options.keyboard [boolean]
(default true)
|
If
true
, keyboard shortcuts are enabled (see
KeyboardHandler
).
|
options.doubleClickZoom [boolean]
(default true)
|
If
true
, the "double click to zoom" interaction is enabled (see
DoubleClickZoomHandler
).
|
options.touchZoomRotate [boolean]
(default true)
|
If
true
, the "pinch to rotate and zoom" interaction is enabled (see
TouchZoomRotateHandler
).
|
options.trackResize [boolean]
(default true)
|
If
true
, the map will automatically resize when the browser window resizes.
|
options.center [LngLatLike]
(default [0,0])
|
The inital geographical centerpoint of the map. If
center
is not specified in the constructor options, Mapbox GL JS will look for it in the map's style object. If it is not specified in the style, either, it will default to
[0, 0]
.
|
options.zoom [number]
(default 0)
|
The initial zoom level of the map. If
zoom
is not specified in the constructor options, Mapbox GL JS will look for it in the map's style object. If it is not specified in the style, either, it will default to
0
.
|
options.bearing [number]
(default 0)
|
The initial bearing (rotation) of the map, measured in degrees counter-clockwise from north. If
bearing
is not specified in the constructor options, Mapbox GL JS will look for it in the map's style object. If it is not specified in the style, either, it will default to
0
.
|
options.pitch [number]
(default 0)
|
The initial pitch (tilt) of the map, measured in degrees away from the plane of the screen (0-60). If
pitch
is not specified in the constructor options, Mapbox GL JS will look for it in the map's style object. If it is not specified in the style, either, it will default to
0
.
|
Example
var map = new mapboxgl.Map({
container: 'map',
center: [-122.420679, 37.772537],
zoom: 13,
style: style_object,
hash: true
});
Instance Members
addControl
(control, [position])
Adds a IControl to the map, calling control.onAdd(this).
Parameters
position ([string]) position on the map to which the control will be added.
Valid values are
'top-left'
,
'top-right'
,
'bottom-left'
, and
'bottom-right'
. Defaults to
'top-right'
.
Returns
Map:
this
Related
Removes the control from the map.
Parameters
Returns
Map:
this
addClass
(klass, [options])
Adds a Mapbox style class to the map.
Keep in mind that these classes are used for controlling a style layer's paint properties, so are not reflected
in an HTML element's class attribute. To learn more about Mapbox style classes, read about
Layers in the style specification.
Parameters
klass (string) The style class to add.
Returns
Map:
this
removeClass
(klass, [options])
Removes a Mapbox style class from the map.
Parameters
klass (string) The style class to remove.
Returns
Map:
this
setClasses
(klasses, [options])
Replaces the map's existing Mapbox style classes with a new array of classes.
Parameters
Returns
Map:
this
Returns a Boolean indicating whether the map has the
specified Mapbox style class.
Parameters
klass (string) The style class to test.
Returns
boolean:
true
if the map has the specified style class.
Returns the map's Mapbox style classes.
Returns
Array<string>:
The map's style classes.
Resizes the map according to the dimensions of its
container element.
This method must be called after the map's container is resized by another script,
or when the map is shown after being initially hidden with CSS.
Returns
Map:
this
Returns the map's geographical bounds.
Returns
LngLatBounds:
The map's geographical bounds.
setMaxBounds
(lnglatbounds)
Sets or clears the map's geographical bounds.
Pan and zoom operations are constrained within these bounds.
If a pan or zoom is performed that would
display regions outside these bounds, the map will
instead display a position and zoom level
as close as possible to the operation's request while still
remaining within the bounds.
Parameters
lnglatbounds ((LngLatBoundsLike | null | undefined)) The maximum bounds to set. If
null
or
undefined
is provided, the function removes the map's maximum bounds.
Returns
Map:
this
Sets or clears the map's minimum zoom level.
If the map's current zoom level is lower than the new minimum,
the map will zoom to the new minimum.
Parameters
minZoom (?number) The minimum zoom level to set (0-20).
If
null
or
undefined
is provided, the function removes the current minimum zoom (i.e. sets it to 0).
Returns
Map:
this
Returns the map's minimum allowable zoom level.
Returns
number:
minZoom
Sets or clears the map's maximum zoom level.
If the map's current zoom level is higher than the new maximum,
the map will zoom to the new maximum.
Parameters
maxZoom (?number) The maximum zoom level to set.
If
null
or
undefined
is provided, the function removes the current maximum zoom (sets it to 20).
Returns
Map:
this
Returns the map's maximum allowable zoom level.
Returns
number:
maxZoom
Returns a Point representing pixel coordinates, relative to the map's container,
that correspond to the specified geographical location.
Parameters
lnglat (LngLatLike) The geographical location to project.
Returns
Point:
The
Point
corresponding to
lnglat
, relative to the map's
container
.
Returns a LngLat representing geographical coordinates that correspond
to the specified pixel coordinates.
Parameters
point (PointLike) The pixel coordinates to unproject.
Returns
LngLat:
The
LngLat
corresponding to
point
.
Related
queryRenderedFeatures
([geometry], [parameters])
Returns an array of GeoJSON
Feature objects
representing visible features that satisfy the query parameters.
Parameters
geometry ([(PointLike | Array<PointLike>)]) The geometry of the query region:
either a single point or southwest and northeast points describing a bounding box.
Omitting this parameter (i.e. calling
Map#queryRenderedFeatures
with zero arguments,
or with only a
parameters
argument) is equivalent to passing a bounding box encompassing the entire
map viewport.
| Name |
Description |
parameters.layers [Array<string>]
|
An array of style layer IDs for the query to inspect.
Only features within these layers will be returned. If this parameter is undefined, all layers will be checked.
|
parameters.filter [Array]
|
A
filter
to limit query results.
|
Returns
Array<Object>:
An array of
GeoJSON
feature objects
.
The properties value of each returned feature object contains the properties of its source feature. For GeoJSON sources, only
string and numeric property values are supported (i.e. null, Array, and Object values are not supported).
Each feature includes a top-level layer property whose value is an object representing the style layer to
which the feature belongs. Layout and paint properties in this object contain values which are fully evaluated
for the given zoom level and feature.
Only visible features are returned. The topmost rendered feature appears first in the returned array, and
subsequent features are sorted by descending z-order. Features that are rendered multiple times (due to wrapping
across the antimeridian at low zoom levels) are returned only once (though subject to the following caveat).
Because features come from tiled vector data or GeoJSON data that is converted to tiles internally, feature
geometries may be split or duplicated across tile boundaries and, as a result, features may appear multiple
times in query results. For example, suppose there is a highway running through the bounding rectangle of a query.
The results of the query will be those parts of the highway that lie within the map tiles covering the bounding
rectangle, even if the highway extends into other tiles, and the portion of the highway within each map tile
will be returned as a separate feature. Similarly, a point feature near a tile boundary may appear in multiple
tiles due to tile buffering.
Example
var features = map.queryRenderedFeatures(
[20, 35],
{ layers: ['my-layer-name'] }
);
var features = map.queryRenderedFeatures(
[[10, 20], [30, 50]],
{ layers: ['my-layer-name'] }
);
var width = 10;
var height = 20;
var features = map.queryRenderedFeatures([
[point.x - width / 2, point.y - height / 2],
[point.x + width / 2, point.y + height / 2]
], { layers: ['my-layer-name'] });
var features = map.queryRenderedFeatures({ layers: ['my-layer-name'] });
Related
querySourceFeatures
(sourceID, [parameters])
Returns an array of GeoJSON
Feature objects
representing features within the specified vector tile or GeoJSON source that satisfy the query parameters.
Parameters
sourceID (string) The ID of the vector tile or GeoJSON source to query.
| Name |
Description |
parameters.sourceLayer [string]
|
The name of the vector tile layer to query.
For vector tile
sources, this parameter is required.
For GeoJSON sources, it is ignored.
|
parameters.filter [Array]
|
A
filter
to limit query results.
|
Returns
Array<Object>:
An array of
GeoJSON
Feature objects
.
In contrast to Map#queryRenderedFeatures, this function
returns all features matching the query parameters,
whether or not they are rendered by the current style (i.e. visible). The domain of the query includes all currently-loaded
vector tiles and GeoJSON source tiles: this function does not check tiles outside the currently
visible viewport.
Because features come from tiled vector data or GeoJSON data that is converted to tiles internally, feature
geometries may be split or duplicated across tile boundaries and, as a result, features may appear multiple
times in query results. For example, suppose there is a highway running through the bounding rectangle of a query.
The results of the query will be those parts of the highway that lie within the map tiles covering the bounding
rectangle, even if the highway extends into other tiles, and the portion of the highway within each map tile
will be returned as a separate feature. Similarly, a point feature near a tile boundary may appear in multiple
tiles due to tile buffering.
Related
setStyle
(style, [options])
Updates the map's Mapbox style object with a new value. If the given
value is style JSON object, compares it against the the map's current
state and perform only the changes necessary to make the map style match
the desired state.
Parameters
| Name |
Description |
options.diff [boolean]
(default true)
|
If false, force a 'full' update, removing the current style
and adding building the given one instead of attempting a diff-based update.
|
Returns
Map:
this
Related
Returns the map's Mapbox style object, which can be used to recreate the map's style.
Returns
Object:
The map's style object.
Adds a source to the map's style.
Parameters
id (string) The ID of the source to add. Must not conflict with existing sources.
| Name |
Description |
source.type string
|
The source type, which must be either one of the core Mapbox GL source types defined in the style specification or a custom type that has been added to the map with
Map#addSourceType
.
|
Returns
Map:
this
Related
Removes a source from the map's style.
Parameters
id (string) The ID of the source to remove.
Returns
Map:
this
Returns the source with the specified ID in the map's style.
Parameters
id (string) The ID of the source to get.
Returns
?Object:
The style source with the specified ID, or
undefined
if the ID corresponds to no existing sources.
Related
addLayer
(layer, [before])
Adds a Mapbox style layer
to the map's style.
A layer defines styling for data from a specified source.
Parameters
before ([string]) The ID of an existing layer to insert the new layer before.
If this argument is omitted, the layer will be appended to the end of the layers array.
Returns
Map:
this
Related
moveLayer
(id, [beforeId])
Moves a layer to a different z-position.
Parameters
id (string) The ID of the layer to move.
beforeId ([string]) The ID of an existing layer to insert the new layer before.
If this argument is omitted, the layer will be appended to the end of the layers array.
Returns
Map:
this
Removes a layer from the map's style.
Parameters
id (string) The ID of the layer to remove.
Returns
Map:
this
Throws
- Error: if no layer with the specified
id
exists.
Returns the layer with the specified ID in the map's style.
Parameters
id (string) The ID of the layer to get.
Returns
?Object:
The layer with the specified ID, or
undefined
if the ID corresponds to no existing layers.
Related
setFilter
(layer, filter)
Sets the filter for the specified style layer.
Parameters
layer (string) The ID of the layer to which the filter will be applied.
Returns
Map:
this
Example
map.setFilter('my-layer', ['==', 'name', 'USA']);
Related
setLayerZoomRange
(layerId, minzoom, maxzoom)
Sets the zoom extent for the specified style layer.
Parameters
layerId (string) The ID of the layer to which the zoom extent will be applied.
minzoom (number) The minimum zoom to set (0-20).
maxzoom (number) The maximum zoom to set (0-20).
Returns
Map:
this
Example
map.setLayerZoomRange('my-layer', 2, 5);
Returns the filter applied to the specified style layer.
Parameters
layer (string) The ID of the style layer whose filter to get.
Returns
Array:
The layer's filter.
setPaintProperty
(layer, name, value, [klass])
Sets the value of a paint property in the specified style layer.
Parameters
layer (string) The ID of the layer to set the paint property in.
name (string) The name of the paint property to set.
value (any) The value of the paint propery to set.
Must be of a type appropriate for the property, as defined in the
Mapbox Style Specification
.
klass ([string]) A style class specifier for the paint property.
Returns
Map:
this
Example
map.setPaintProperty('my-layer', 'fill-color', '#faafee');
Related
getPaintProperty
(layer, name, [klass])
Returns the value of a paint property in the specified style layer.
Parameters
layer (string) The ID of the layer to get the paint property from.
name (string) The name of a paint property to get.
klass ([string]) A class specifier for the paint property.
Returns
any:
The value of the specified paint property.
setLayoutProperty
(layer, name, value)
Sets the value of a layout property in the specified style layer.
Parameters
layer (string) The ID of the layer to set the layout property in.
name (string) The name of the layout property to set.
value (any) The value of the layout propery. Must be of a type appropriate for the property, as defined in the
Mapbox Style Specification
.
Returns
Map:
this
Example
map.setLayoutProperty('my-layer', 'visibility', 'none');
getLayoutProperty
(layer, name)
Returns the value of a layout property in the specified style layer.
Parameters
layer (string) The ID of the layer to get the layout property from.
name (string) The name of the layout property to get.
Returns
any:
The value of the specified layout property.
setLight
(options, lightOptions)
Sets the any combination of light values.
Parameters
Returns
Map:
this
Returns the value of the light object.
Returns
Object:
light Light properties of the style.
Returns the map's containing HTML element.
Returns
HTMLElement:
The map's container.
Returns the HTML element containing the map's <canvas> element.
If you want to add non-GL overlays to the map, you should append them to this element.
This is the element to which event bindings for map interactivity (such as panning and zooming) are
attached. It will receive bubbled events from child elements such as the <canvas>, but not from
map controls.
Returns
HTMLElement:
The container of the map's
<canvas>
.
Related
Returns the map's <canvas> element.
Returns
HTMLCanvasElement:
The map's
<canvas>
element.
Related
Returns a Boolean indicating whether the map is fully loaded.
Returns false if the style is not yet fully loaded,
or if there has been a change to the sources or style that
has not yet fully loaded.
Returns
boolean:
A Boolean indicating whether the map is fully loaded.
Destroys the map's underlying resources, including web workers and DOM elements.
After calling this method, you must not call any other methods on the map.
Gets and sets a Boolean indicating whether the map will render an outline
around each tile. These tile boundaries are useful for debugging.
Gets and sets a Boolean indicating whether the map will render boxes
around all symbols in the data source, revealing which symbols
were rendered or which were hidden due to collisions.
This information is useful for debugging.
Gets and sets a Boolean indicating whether the map will
continuously repaint. This information is useful for analyzing performance.
Returns the map's geographical centerpoint.
Returns
LngLat:
The map's geographical centerpoint.
setCenter
(center, [eventData])
Sets the map's geographical centerpoint. Equivalent to jumpTo({center: center}).
Parameters
eventData ([Object]) Data to propagate to any event listeners.
Returns
Map:
this
Example
map.setCenter([-74, 38]);
Related
panBy
(offset, [options], [eventData])
Pans the map by the specified offest.
Parameters
offset (Array<number>) x
and
y
coordinates by which to pan the map.
eventData ([Object]) Data to propagate to any event listeners.
Returns
Map:
this
Related
panTo
(lnglat, [options], [eventData])
Pans the map to the specified location, with an animated transition.
Parameters
lnglat (LngLatLike) The location to pan the map to.
eventData ([Object]) Data to propagate to any event listeners.
Returns
Map:
this
Returns the map's current zoom level.
Returns
number:
The map's current zoom level.
setZoom
(zoom, [eventData])
Sets the map's zoom level. Equivalent to jumpTo({zoom: zoom}).
Parameters
zoom (number) The zoom level to set (0-20).
eventData ([Object]) Data to propagate to any event listeners.
Returns
Map:
this
Example
map.setZoom(5);
zoomTo
(zoom, [options], [eventData])
Zooms the map to the specified zoom level, with an animated transition.
Parameters
zoom (number) The zoom level to transition to.
eventData ([Object]) Data to propagate to any event listeners.
Returns
Map:
this
zoomIn
([options], [eventData])
Increases the map's zoom level by 1.
Parameters
eventData ([Object]) Data to propagate to any event listeners.
Returns
Map:
this
zoomOut
([options], [eventData])
Decreases the map's zoom level by 1.
Parameters
eventData ([Object]) Data to propagate to any event listeners.
Returns
Map:
this
Returns the map's current bearing (rotation).
Returns
number:
The map's current bearing, measured in degrees counter-clockwise from north.
Related
setBearing
(bearing, [eventData])
Sets the maps' bearing (rotation). Equivalent to jumpTo({bearing: bearing}).
Parameters
bearing (number) The bearing to set, measured in degrees counter-clockwise from north.
eventData ([Object]) Data to propagate to any event listeners.
Returns
Map:
this
Example
map.setBearing(90);
rotateTo
(bearing, [options], [eventData])
Rotates the map to the specified bearing, with an animated transition.
Parameters
bearing (number) The bearing to rotate the map to, measured in degrees counter-clockwise from north.
eventData ([Object]) Data to propagate to any event listeners.
Returns
Map:
this
resetNorth
([options], [eventData])
Rotates the map to a bearing of 0 (due north), with an animated transition.
Parameters
eventData ([Object]) Data to propagate to any event listeners.
Returns
Map:
this
snapToNorth
([options], [eventData])
Snaps the map's bearing to 0 (due north), if the current bearing is close enough to it (i.e. within the bearingSnap threshold).
Parameters
eventData ([Object]) Data to propagate to any event listeners.
Returns
Map:
this
Returns the map's current pitch (tilt).
Returns
number:
The map's current pitch, measured in degrees away from the plane of the screen.
setPitch
(pitch, [eventData])
Sets the map's pitch (tilt). Equivalent to jumpTo({pitch: pitch}).
Parameters
pitch (number) The pitch to set, measured in degrees away from the plane of the screen (0-60).
eventData ([Object]) Data to propagate to any event listeners.
Returns
Map:
this
fitBounds
(bounds, [options], [eventData])
Pans and zooms the map to contain its visible area within the specified geographical bounds.
Parameters
bounds (LngLatBoundsLike) Center these bounds in the viewport and use the highest
zoom level up to and including
Map#getMaxZoom()
that fits them in the viewport.
| Name |
Description |
options.linear [boolean]
(default false)
|
If
true
, the map transitions using
Map#easeTo
. If
false
, the map transitions using
Map#flyTo
. See
Map#flyTo
for information about the options specific to that animated transition.
|
options.easing [Function]
|
An easing function for the animated transition.
|
options.padding [number]
(default 0)
|
The amount of padding, in pixels, to allow around the specified bounds.
|
options.offset [PointLike]
(default [0,0])
|
The center of the given bounds relative to the map's center, measured in pixels.
|
options.maxZoom [number]
|
The maximum zoom level to allow when the map view transitions to the specified bounds.
|
eventData ([Object]) Data to propagate to any event listeners.
Returns
Map:
this
Related
jumpTo
(options, [eventData])
Changes any combination of center, zoom, bearing, and pitch, without
an animated transition. The map will retain its current values for any
details not specified in options.
Parameters
eventData ([Object]) Data to propagate to any event listeners.
Returns
Map:
this
easeTo
(options, [eventData])
Changes any combination of center, zoom, bearing, and pitch, with an animated transition
between old and new values. The map will retain its current values for any
details not specified in options.
Parameters
eventData ([Object]) Data to propagate to any event listeners.
Returns
Map:
this
Related
flyTo
(options, [eventData])
Changes any combination of center, zoom, bearing, and pitch, animating the transition along a curve that
evokes flight. The animation seamlessly incorporates zooming and panning to help
the user maintain her bearings even after traversing a great distance.
Parameters
| Name |
Description |
options.curve [number]
(default 1.42)
|
The zooming "curve" that will occur along the
flight path. A high value maximizes zooming for an exaggerated animation, while a low
value minimizes zooming for an effect closer to
Map#easeTo
. 1.42 is the average
value selected by participants in the user study discussed in
van Wijk (2003)
. A value of
Math.pow(6, 0.25)
would be equivalent to the root mean squared average velocity. A
value of 1 would produce a circular motion.
|
options.minZoom [number]
|
The zero-based zoom level at the peak of the flight path. If
options.curve
is specified, this option is ignored.
|
options.speed [number]
(default 1.2)
|
The average speed of the animation defined in relation to
options.curve
. A speed of 1.2 means that the map appears to move along the flight path
by 1.2 times
options.curve
screenfuls every second. A
screenful
is the map's visible span.
It does not correspond to a fixed physical distance, but varies by zoom level.
|
options.screenSpeed [number]
|
The average speed of the animation measured in screenfuls
per second, assuming a linear timing curve. If
options.speed
is specified, this option is ignored.
|
options.easing [Function]
|
An easing function for the animated transition.
|
eventData ([Object]) Data to propagate to any event listeners.
Returns
Map:
this
Example
map.flyTo({center: [0, 0], zoom: 9});
map.flyTo({
center: [0, 0],
zoom: 9,
speed: 0.2,
curve: 1,
easing(t) {
return t;
}
});
Related
Stops any animated transition underway.
Returns
Map:
this
Events
Fired immediately after the map has been resized.
Fired when the WebGL context is lost.
Parameters
Properties
Fired when the WebGL context is restored.
Parameters
Properties
Fired immediately after the map has been removed with Map#remove.
Fired when a pointing device (usually a mouse) is released within the map.
Properties
Related
Fired when a pointing device (usually a mouse) is pressed within the map.
Properties
Related
Fired when a point device (usually a mouse) leaves the map's canvas.
Properties
Related
Fired whenever the map is drawn to the screen, as the result of
- a change to the map's position, zoom, pitch, or bearing
- a change to the map's style
- a change to a GeoJSON source
- the loading of a vector tile, GeoJSON file, glyph, or sprite
Fired when one of the map's sources' tiles begins loading or changing
asyncronously. All tiledataloading events are followed by a tiledata
or error event. See MapDataEvent for more information.
Properties
Fired just before the map begins a transition from one
view to another, as the result of either user interaction or methods such as Map#jumpTo.
Properties
Fired when a pointing device (usually a mouse) is clicked twice at the same point on the map.
Properties
Fired when a pointing device (usually a mouse) is pressed and released at the same point on the map.
Properties
Related
Fired when a touch point has been disrupted.
Properties
Fired when a touch point is moved within the map.
Properties
Fired when a touch point is removed from the map.
Properties
Fired when a touch point is placed on the map.
Properties
Fired when one of the map's sources begins loading or changing asyncronously.
This event is not fired if a tile belonging to a source begins loading or
changing (that is handled by tiledataloading). All sourcedataloading
events are followed by a sourcedata or error event. See
MapDataEvent for more information.
Properties
Fired when the map's style begins loading or changing asyncronously.
All styledataloading events are followed by a styledata
or error event. See MapDataEvent for more information.
Properties
Fired when a pointing device (usually a mouse) is moved within the map.
Properties
Related
Fired immediately after all necessary resources have been downloaded
and the first visually complete rendering of the map has occurred.
Related
Fired repeatedly during an animated transition from one view to
another, as the result of either user interaction or methods such as Map#flyTo.
Properties
Fired just after the map completes a transition from one
view to another, as the result of either user interaction or methods such as Map#jumpTo.
Properties
Related
Fired when an error occurs. This is GL JS's primary error reporting
mechanism. We use an event instead of throw to better accommodate
asyncronous operations. If no listeners are bound to the error event, the
error will be printed to the console.
Properties
data ({error: {message: string}})
Fired when any map data loads or changes. See MapDataEvent
for more information.
Properties
Fired when the map's style loads or changes. See
MapDataEvent for more information.
Properties
Fired when one of the map's sources loads or changes. This event is not fired
if a tile belonging to a source loads or changes (that is handled by
tiledata). See MapDataEvent for more information.
Properties
Fired when any map data (style, source, tile, etc) begins loading or
changing asyncronously. All dataloading events are followed by a data
or error event. See MapDataEvent for more information.
Properties
Fired when one of the map's sources' tiles loads or changes. See
MapDataEvent for more information.
Properties
Fired just after the map completes a transition from one zoom level to another,
as the result of either user interaction or methods such as Map#flyTo.
Properties
Fired repeatedly during an animated transition from one zoom level to another,
as the result of either user interaction or methods such as Map#flyTo.
Properties
Related
Fired just before the map begins a transition from one zoom level to another,
as the result of either user interaction or methods such as Map#flyTo.
Properties
Fired when a "box zoom" interaction starts. See BoxZoomHandler.
Properties
Fired when the user cancels a "box zoom" interaction, or when the bounding box does not meet the minimum size threshold.
See BoxZoomHandler.
Properties
Fired when a "box zoom" interaction ends. See BoxZoomHandler.
Properties
Fired repeatedly during a "drag to rotate" interaction. See DragRotateHandler.
Properties
Fired when a "drag to pan" interaction ends. See DragPanHandler.
Properties
Fired repeatedly during a "drag to pan" interaction. See DragPanHandler.
Properties
Fired when a "drag to pan" interaction starts. See DragPanHandler.
Properties
Fired whenever the map's pitch (tilt) changes.
Properties
Related
Gets and sets the map's access token.
Example
mapboxgl.accessToken = myAccessToken;
Related
Returns a Boolean indicating whether the browser supports Mapbox GL JS.
Parameters
| Name |
Description |
options.failIfMajorPerformanceCaveat [boolean]
(default false)
|
If
true
,
the function will return
false
if the performance of Mapbox GL JS would
be dramatically worse than expected (i.e. a software renderer would be used).
|
Returns
boolean:
Example
mapboxgl.supported()
Related
The version of Mapbox GL JS in use as specified in package.json,
CHANGELOG.md, and the GitHub release.
Geography
These are Mapbox GL JS's ways of representing locations
and areas on the sphere.
A LngLat object represents a given longitude and latitude coordinate, measured in degrees.
Mapbox GL uses longitude, latitude coordinate order (as opposed to latitude, longitude) to match GeoJSON.
Note that any Mapbox GL method that accepts a LngLat object as an argument or option
can also accept an Array of two numbers and will perform an implicit conversion.
This flexible type is documented as LngLatLike.
Parameters
lng (number) Longitude, measured in degrees.
lat (number) Latitude, measured in degrees.
Example
var ll = new mapboxgl.LngLat(-73.9749, 40.7736);
Static Members
Converts an array of two numbers to a LngLat object.
If a LngLat object is passed in, the function returns it unchanged.
Parameters
input (LngLatLike) An array of two numbers to convert, or a
LngLat
object to return.
Returns
LngLat:
A new
LngLat
object, if a conversion occurred, or the original
LngLat
object.
Example
var arr = [-73.9749, 40.7736];
var ll = mapboxgl.LngLat.convert(arr);
ll;
Instance Members
Returns a new LngLat object whose longitude is wrapped to the range (-180, 180).
Returns
LngLat:
The wrapped
LngLat
object.
Example
var ll = new mapboxgl.LngLat(286.0251, 40.7736);
var wrapped = ll.wrap();
wrapped.lng;
Returns the coordinates represented as an array of two numbers.
Returns
Array<number>:
The coordinates represeted as an array of longitude and latitude.
Example
var ll = new mapboxgl.LngLat(-73.9749, 40.7736);
ll.toArray();
Returns the coordinates represent as a string.
Returns
string:
The coordinates represented as a string of the format
'LngLat(lng, lat)'
.
Example
var ll = new mapboxgl.LngLat(-73.9749, 40.7736);
ll.toString();
Related
A LngLatBounds object represents a geographical bounding box,
defined by its southwest and northeast points in longitude and latitude.
If no arguments are provided to the constructor, a null bounding box is created.
Note that any Mapbox GL method that accepts a LngLatBounds object as an argument or option
can also accept an Array of two LngLatLike constructs and will perform an implicit conversion.
This flexible type is documented as LngLatBoundsLike.
Parameters
sw ([LngLatLike]) The southwest corner of the bounding box.
ne ([LngLatLike]) The northeast corner of the bounding box.
Example
var sw = new mapboxgl.LngLat(-73.9876, 40.7661);
var ne = new mapboxgl.LngLat(-73.9397, 40.8002);
var llb = new mapboxgl.LngLatBounds(sw, ne);
Static Members
Converts an array to a LngLatBounds object.
If a LngLatBounds object is passed in, the function returns it unchanged.
Internally, the function calls LngLat#convert to convert arrays to LngLat values.
Parameters
input (LngLatBoundsLike) An array of two coordinates to convert, or a
LngLatBounds
object to return.
Returns
LngLatBounds:
A new
LngLatBounds
object, if a conversion occurred, or the original
LngLatBounds
object.
Example
var arr = [[-73.9876, 40.7661], [-73.9397, 40.8002]];
var llb = mapboxgl.LngLatBounds.convert(arr);
llb;
Instance Members
Set the northeast corner of the bounding box
Parameters
Returns
LngLatBounds:
this
Set the southwest corner of the bounding box
Parameters
Returns
LngLatBounds:
this
Extend the bounds to include a given LngLat or LngLatBounds.
Parameters
Returns
LngLatBounds:
this
Returns the geographical coordinate equidistant from the bounding box's corners.
Returns
LngLat:
The bounding box's center.
Example
var llb = new mapboxgl.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]);
llb.getCenter();
Returns the southwest corner of the bounding box.
Returns
LngLat:
The southwest corner of the bounding box.
Returns the northeast corner of the bounding box.
Returns
LngLat:
The northeast corner of the bounding box.
Returns the northwest corner of the bounding box.
Returns
LngLat:
The northwest corner of the bounding box.
Returns the southeast corner of the bounding box.
Returns
LngLat:
The southeast corner of the bounding box.
Returns the west edge of the bounding box.
Returns
number:
The west edge of the bounding box.
Returns the south edge of the bounding box.
Returns
number:
The south edge of the bounding box.
Returns the east edge of the bounding box.
Returns
number:
The east edge of the bounding box.
Returns the north edge of the bounding box.
Returns
number:
The north edge of the bounding box.
Returns the bounding box represented as an array.
Returns
Array<Array<number>>:
The bounding box represented as an array, consisting of the
southwest and northeast coordinates of the bounding represented as arrays of numbers.
Example
var llb = new mapboxgl.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]);
llb.toArray();
Return the bounding box represented as a string.
Returns
string:
The bounding box represents as a string of the format
'LngLatBounds(LngLat(lng, lat), LngLat(lng, lat))'
.
Example
var llb = new mapboxgl.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]);
llb.toString();
Controls
Controls add new functionality and user interface
elements to the map.
A NavigationControl control contains zoom buttons and a compass.
new NavigationControl()
Example
var nav = new mapboxgl.NavigationControl();
map.addControl(nav, 'top-left');
Related
A GeolocateControl control provides a button that uses the browser's geolocation
API to locate the user on the map.
Not all browsers support geolocation,
and some users may disable the feature. Geolocation support for modern
browsers including Chrome requires sites to be served over HTTPS. If
geolocation support is not available, the GeolocateControl will not
be visible.
Extends Evented.
new GeolocateControl(options: [
Object])
Parameters
| Name |
Description |
options.positionOptions [Object]
(default {enableHighAccuracy:false,timeout:6000})
|
A
PositionOptions
object.
|
options.watchPosition [Object]
(default false)
|
If
true
the map will reposition each time the position of the device changes and the control becomes a toggle.
|
Example
map.addControl(new mapboxgl.GeolocateControl({
positionOptions: {
enableHighAccuracy: true
}
}));
Events
geolocate event.
Properties
An AttributionControl control presents the map's attribution information.
new AttributionControl(options: [
Object])
Parameters
| Name |
Description |
options.compact [boolean]
|
If
true
force a compact attribution that shows the full attribution on mouse hover, or if
false
force the full attribution control. The default is a responsive attribution that collapses when the map is less than 640 pixels wide.
|
Example
var map = new mapboxgl.Map({attributionControl: false})
.addControl(new mapboxgl.AttributionControl({
compact: true
}));
A ScaleControl control displays the ratio of a distance on the map to the corresponding distance on the ground.
new ScaleControl(options: [
Object])
Parameters
| Name |
Description |
options.maxWidth [number]
(default '150')
|
The maximum length of the scale control in pixels.
|
options.unit [string]
(default 'metric')
|
Unit of the distance (
'imperial'
or
'metric'
).
|
Example
map.addControl(new mapboxgl.ScaleControl({
maxWidth: 80,
unit: 'imperial'
}));
Handlers
Handlers add different kinds of interactivity to the map -
mouse interactivity, touch interactions, and other
gestures.
The BoxZoomHandler allows the user to zoom the map to fit within a bounding box.
The bounding box is defined by clicking and holding shift while dragging the cursor.
new BoxZoomHandler(map:
Map)
Parameters
map (Map) The Mapbox GL JS map to add the handler to.
Instance Members
Returns a Boolean indicating whether the "box zoom" interaction is enabled.
Returns
boolean:
true
if the "box zoom" interaction is enabled.
Returns a Boolean indicating whether the "box zoom" interaction is active, i.e. currently being used.
Returns
boolean:
true
if the "box zoom" interaction is active.
Enables the "box zoom" interaction.
Example
map.boxZoom.enable();
Disables the "box zoom" interaction.
Example
map.boxZoom.disable();
The ScrollZoomHandler allows the user to zoom the map by scrolling.
new ScrollZoomHandler(map:
Map)
Parameters
map (Map) The Mapbox GL JS map to add the handler to.
Instance Members
The DragPanHandler allows the user to pan the map by clicking and dragging
the cursor.
new DragPanHandler(map:
Map)
Parameters
map (Map) The Mapbox GL JS map to add the handler to.
Instance Members
Returns a Boolean indicating whether the "drag to pan" interaction is enabled.
Returns
boolean:
true
if the "drag to pan" interaction is enabled.
Returns a Boolean indicating whether the "drag to pan" interaction is active, i.e. currently being used.
Returns
boolean:
true
if the "drag to pan" interaction is active.
Enables the "drag to pan" interaction.
Example
map.dragPan.enable();
Disables the "drag to pan" interaction.
Example
map.dragPan.disable();
The DragRotateHandler allows the user to rotate the map by clicking and
dragging the cursor while holding the right mouse button or ctrl key.
new DragRotateHandler(map:
Map, options: [
Object])
Parameters
map (Map) The Mapbox GL JS map to add the handler to.
| Name |
Description |
options.bearingSnap [number]
|
The threshold, measured in degrees, that determines when the map's
bearing (rotation) will snap to north.
|
options.pitchWithRotate [bool]
(default true)
|
Control the map pitch in addition to the bearing
|
Instance Members
Returns a Boolean indicating whether the "drag to rotate" interaction is enabled.
Returns
boolean:
true
if the "drag to rotate" interaction is enabled.
Returns a Boolean indicating whether the "drag to rotate" interaction is active, i.e. currently being used.
Returns
boolean:
true
if the "drag to rotate" interaction is active.
Enables the "drag to rotate" interaction.
Example
map.dragRotate.enable();
Disables the "drag to rotate" interaction.
Example
map.dragRotate.disable();
The KeyboardHandler allows the user to zoom, rotate, and pan the map using
the following keyboard shortcuts:
= / +: Increase the zoom level by 1.
Shift-= / Shift-+: Increase the zoom level by 2.
-: Decrease the zoom level by 1.
Shift--: Decrease the zoom level by 2.
- Arrow keys: Pan by 100 pixels.
Shift+⇢: Increase the rotation by 15 degrees.
Shift+⇠: Decrease the rotation by 15 degrees.
Shift+⇡: Increase the pitch by 10 degrees.
Shift+⇣: Decrease the pitch by 10 degrees.
new KeyboardHandler(map:
Map)
Parameters
map (Map) The Mapbox GL JS map to add the handler to.
Instance Members
Returns a Boolean indicating whether keyboard interaction is enabled.
Returns
boolean:
true
if keyboard interaction is enabled.
Enables keyboard interaction.
Example
map.keyboard.enable();
Disables keyboard interaction.
Example
map.keyboard.disable();
The DoubleClickZoomHandler allows the user to zoom the map at a point by
double clicking.
new DoubleClickZoomHandler(map:
Map)
Parameters
map (Map) The Mapbox GL JS map to add the handler to.
Instance Members
Returns a Boolean indicating whether the "double click to zoom" interaction is enabled.
Returns
boolean:
true
if the "double click to zoom" interaction is enabled.
Enables the "double click to zoom" interaction.
Example
map.doubleClickZoom.enable();
Disables the "double click to zoom" interaction.
Example
map.doubleClickZoom.disable();
The TouchZoomRotateHandler allows the user to zoom and rotate the map by
pinching on a touchscreen.
new TouchZoomRotateHandler(map:
Map)
Parameters
map (Map) The Mapbox GL JS map to add the handler to.
Instance Members
Returns a Boolean indicating whether the "pinch to rotate and zoom" interaction is enabled.
Returns
boolean:
true
if the "pinch to rotate and zoom" interaction is enabled.
Enables the "pinch to rotate and zoom" interaction.
Example
map.touchZoomRotate.enable();
Disables the "pinch to rotate and zoom" interaction.
Example
map.touchZoomRotate.disable();
Disables the "pinch to rotate" interaction, leaving the "pinch to zoom"
interaction enabled.
Example
map.touchZoomRotate.disableRotation();
Enables the "pinch to rotate" interaction.
Example
map.touchZoomRotate.enable();
map.touchZoomRotate.enableRotation();
Sources
Sources specify the geographic features to be rendered on the map. Source
objects may be obtained from Map#getSource.
A source containing GeoJSON.
(See the Style Specification for detailed documentation of options.)
Extends Evented.
Example
map.addSource('some id', {
type: 'geojson',
data: 'https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_10m_ports.geojson'
});
map.addSource('some id', {
type: 'geojson',
data: {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
-76.53063297271729,
39.18174077994108
]
}
}]
}
});
map.getSource('some id').setData({
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": { "name": "Null Island" },
"geometry": {
"type": "Point",
"coordinates": [ 0, 0 ]
}
}]
});
Instance Members
Sets the GeoJSON data and re-renders the map.
Parameters
data ((Object | string)) A GeoJSON data object or a URL to one. The latter is preferable in the case of large GeoJSON files.
Returns
GeoJSONSource:
this
Related
A data source containing video.
(See the Style Specification for detailed documentation of options.)
Extends ImageSource.
Example
map.addSource('some id', {
type: 'video',
url: [
'https://www.mapbox.com/blog/assets/baltimore-smoke.mp4',
'https://www.mapbox.com/blog/assets/baltimore-smoke.webm'
],
coordinates: [
[-76.54, 39.18],
[-76.52, 39.18],
[-76.52, 39.17],
[-76.54, 39.17]
]
});
var mySource = map.getSource('some id');
mySource.setCoordinates([
[-76.54335737228394, 39.18579907229748],
[-76.52803659439087, 39.1838364847587],
[-76.5295386314392, 39.17683392507606],
[-76.54520273208618, 39.17876344106642]
]);
map.removeSource('some id');
Instance Members
Returns the HTML video element.
Returns
HTMLVideoElement:
The HTML
video
element.
setCoordinates
(coordinates)
Sets the video's coordinates and re-renders the map.
Parameters
coordinates (Array<Array<number>>) Four geographical coordinates,
represented as arrays of longitude and latitude numbers, which define the corners of the video.
The coordinates start at the top left corner of the video and proceed in clockwise order.
They do not have to represent a rectangle.
Returns
VideoSource:
this
Related
A data source containing an image.
(See the Style Specification for detailed documentation of options.)
Extends Evented.
Example
map.addSource('some id', {
type: 'image',
url: 'https://www.mapbox.com/images/foo.png',
coordinates: [
[-76.54, 39.18],
[-76.52, 39.18],
[-76.52, 39.17],
[-76.54, 39.17]
]
});
var mySource = map.getSource('some id');
mySource.setCoordinates([
[-76.54335737228394, 39.18579907229748],
[-76.52803659439087, 39.1838364847587],
[-76.5295386314392, 39.17683392507606],
[-76.54520273208618, 39.17876344106642]
]);
map.removeSource('some id');
Instance Members
setCoordinates
(coordinates)
Sets the image's coordinates and re-renders the map.
Parameters
coordinates (Array<Array<number>>) Four geographical coordinates,
represented as arrays of longitude and latitude numbers, which define the corners of the image.
The coordinates start at the top left corner of the image and proceed in clockwise order.
They do not have to represent a rectangle.
Returns
ImageSource:
this
Related
Options
These shared option can be supplied as arguments to various methods
on a Map object and other functions.
Options common to Map#jumpTo, Map#easeTo, and Map#flyTo,
controlling the destination's location, zoom level, bearing, and pitch.
All properties are optional. Unspecified
options will default to the map's current value for that property.
Extends Evented.
Properties
zoom (number)
: The destination's zoom level.
bearing (number)
: The destination's bearing (rotation), measured in degrees counter-clockwise from north.
pitch (number)
: The destination's pitch (tilt), measured in degrees.
around (LngLatLike)
: If a
zoom
is specified,
around
determines the zoom center (defaults to the center of the map).
Options common to map movement methods that involve animation, such as Map#panBy and
Map#easeTo, controlling the duration and easing function of the animation. All properties
are optional.
Extends Evented.
Properties
duration (number)
: The animation's duration, measured in milliseconds.
easing (Function)
: The animation's easing function.
offset (PointLike)
:
x
and
y
coordinates representing the animation's origin of movement relative to the map's center.
animate (boolean)
: If
false
, no animation will occur.
Options common to Map#addClass, Map#removeClass,
and Map#setClasses, controlling
whether or not to smoothly transition property changes triggered by a class change.
Properties
transition (boolean)
: If
true
, property changes will smootly transition.
A popup component.
Extends Evented.
Parameters
| Name |
Description |
options.closeButton [boolean]
(default true)
|
If
true
, a close button will appear in the
top right corner of the popup.
|
options.closeOnClick [boolean]
(default true)
|
If
true
, the popup will closed when the
map is clicked.
|
options.anchor [string]
|
A string indicating the popup's location relative to
the coordinate set via
Popup#setLngLat
.
Options are
'top'
,
'bottom'
,
'left'
,
'right'
,
'top-left'
,
'top-right'
,
'bottom-left'
, and
'bottom-right'
. If unset the anchor will be
dynamically set to ensure the popup falls within the map container with a preference
for
'bottom'
.
|
options.offset [(number | PointLike | Object)]
|
A pixel offset applied to the popup's location specified as:
- a single number specifying a distance from the popup's location
- a
PointLike specifying a constant offset
- an object of
PointLikes specifing an offset for each anchor position
Negative offsets indicate left and up.
|
Example
var markerHeight = 50, markerRadius = 10, linearOffset = 25;
var popupOffsets = {
'top': [0, 0],
'top-left': [0,0],
'top-right': [0,0],
'bottom': [0, -markerHeight],
'bottom-left': [linearOffset, (markerHeight - markerRadius + linearOffset) * -1],
'bottom-right': [-linearOffset, (markerHeight - markerRadius + linearOffset) * -1],
'left': [markerRadius, (markerHeight - markerRadius) * -1],
'right': [-markerRadius, (markerHeight - markerRadius) * -1]
};
var popup = new mapboxgl.Popup({offset:popupOffsets})
.setLngLat(e.lngLat)
.setHTML("<h1>Hello World!</h1>")
.addTo(map);
Instance Members
Events
Related
Creates a marker component
Parameters
element ([HTMLElement]) DOM element to use as a marker (creates a div element by default)
| Name |
Description |
options.offset [PointLike]
|
The offset in pixels as a
PointLike
object to apply relative to the element's top left corner. Negatives indicate left and up.
|
Example
var marker = new mapboxgl.Marker()
.setLngLat([30.5, 50.5])
.addTo(map);
Instance Members
Attaches the marker to a map
Parameters
Returns
Marker:
this
Removes the marker from a map
Returns
Marker:
this
Example
var marker = new mapboxgl.Marker().addTo(map);
marker.remove();
Get the marker's geographical location
Returns
LngLat:
Set the marker's geographical position and move it.
Parameters
Returns
Marker:
this
Related
Events
This event functionality is used by Mapbox GL JS itself
and can be useful to other developers who want to create
IControls or other extensions.
Methods mixed in to other classes for event capabilities.
Instance Members
Adds a listener to a specified event type.
Parameters
type (string) The event type to add a listen for.
listener (Function) The function to be called when the event is fired.
The listener function is called with the data object passed to
fire
,
extended with
target
and
type
properties.
Returns
Object:
this
Removes a previously registered event listener.
Parameters
type (string) The event type to remove listeners for.
listener (Function) The listener function to remove.
Returns
Object:
this
Adds a listener that will be called only once to a specified event type.
The listener will be called first time the event fires after the listener is registered.
Parameters
type (string) The event type to listen for.
listener (Function) The function to be called when the event is fired the first time.
Returns
Object:
this
Fires an event of the specified type.
Parameters
type (string) The type of event to fire.
data ([Object]) Data to be passed to any listeners.
Returns
Object:
this
Returns a true if this instance of Evented or any forwardeed instances of Evented have a listener for the specified type.
Parameters
Returns
boolean:
true
if there is at least one registered listener for specified event type,
false
otherwise
Properties
type (string)
: The event type.
target (Map)
: The
Map
object that fired the event.
point (Point)
: The pixel coordinates of the mouse event target, relative to the map
and measured from the top left corner.
lngLat (LngLat)
: The geographic location on the map of the mouse event target.
Properties
type (string)
: The event type.
target (Map)
: The
Map
object that fired the event.
point (Point)
: The pixel coordinates of the center of the touch event points, relative to the map
and measured from the top left corner.
lngLat (LngLat)
: The geographic location on the map of the center of the touch event points.
Properties
boxZoomBounds (LngLatBounds)
: The bounding box of the "box zoom" interaction.
This property is only provided for
boxzoomend
events.
A MapDataEvent object is emitted with the Map#data
and Map#dataloading events. Possible values for
dataTypes are:
'source': The non-tile data associated with any source
'style': The style used by the map
'tile': A vector or raster tile
Properties
type (string)
: The event type.
dataType (string)
: The type of data that has changed. One of
'source'
,
'style'
.
isSourceLoaded ([boolean])
: True if the event has a
dataType
of
source
and the source has no outstanding network requests.
coord ([Coordinate])
: The coordinate of the tile if the event has a
dataType
of
tile
.
Types
These are types used in the documentation above to describe arguments,
properties, and return values.
A LngLat object or an array of two numbers representing longitude and latitude.
Example
var v1 = new mapboxgl.LngLat(-122.420679, 37.772537);
var v2 = [-122.420679, 37.772537];
A LngLatBounds object or an array of LngLatLike objects.
Example
var v1 = new mapboxgl.LngLatBounds(
new mapboxgl.LngLat(-73.9876, 40.7661),
new mapboxgl.LngLat(-73.9397, 40.8002)
);
var v2 = new mapboxgl.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002])
var v3 = [[-73.9876, 40.7661], [-73.9397, 40.8002]];
A Point geometry object, which has
x and y properties representing coordinates.
A Point or an array of two numbers representing x and y coordinates.
Interfaces
Interfaces are specifications for plugin authors to follow in order to
create new extensions to Mapbox GL JS
Interface for interactive controls added to the map. This is an
specification for implementers to model: it is not
an exported method or class.
Controls must implement onAdd and onRemove, and must own an
element, which is often a div element. To use Mapbox GL JS's
default control styling, add the mapboxgl-ctrl class to your control's
node.
Example
class HelloWorldControl {
onAdd(map) {
this._map = map;
this._container = document.createElement('div');
this._container.className = 'mapboxgl-ctrl';
this._container.textContent = 'Hello, world';
return this._container;
}
onRemove() {
this._container.parentNode.removeChild(this._container);
this._map = undefined;
}
}
function HelloWorldControl() { }
HelloWorldControl.prototype.onAdd = function(map) {
this._map = map;
this._container = document.createElement('div');
this._container.className = 'mapboxgl-ctrl';
this._container.textContent = 'Hello, world';
return this._container;
};
HelloWorldControl.prototype.onRemove() {
this._container.parentNode.removeChild(this._container);
this._map = undefined;
};
Instance Members
Optionally provide a default position for this control. If this method
is implemented and Map#addControl is called without the position
parameter, the value returned by getDefaultPosition will be used as the
control's position.
Returns
string:
a control position, one of the values valid in addControl.
Unregister a control on the map and give it a chance to detach event listeners
and resources. This method is called by Map#removeControl
internally.
Parameters
map (Map) the Map this control will be removed from
Returns
undefined:
there is no required return value for this method
Register a control on the map and give it a chance to register event listeners
and resources. This method is called by Map#addControl
internally.
Parameters
map (Map) the Map this control will be added to
Returns
HTMLElement:
The control's container element. This should
be created by the control and returned by onAdd without being attached
to the DOM: the map will insert the control's element into the DOM
as necessary.