Documentation - map-ir/mapir-react-component GitHub Wiki
import Mapir from "mapir-react-component";
import 'mapir-react-component/dist/index.css';
...
const Map = Mapir.setToken({
transformRequest: (url)=> {
return {
url: url,
headers: {
'x-api-key': 'Your_API_KEY', //Mapir api key
'Mapir-SDK': 'reactjs'
}
}
}
});
...
<Mapir Map={Map} apiKey={'Your_API_Key'}/>these parameters should be initialized in the setToken method.
-
apiUrl:
stringDefine a custom URL to fetch the vector tiles. -
minZoom (Default:
0):numberMinimum zoom level. Must be between 0 and 20. -
maxZoom (Default:
20):numberMaximum zoom level. Must be between 0 and 20. -
scrollZoom (Default:
true):booleanSee mapbox scrollZoom -
hash (Default:
false):boolean, See mapbox doc -
preserveDrawingBuffer (Default:
false):boolean, See mapbox doc -
interactive (Default:
true):booleanSet tofalseto disable interaction with the map. -
attributionControl (Default:
true):booleanSet tofalseto remove the attribution on the map. -
customAttribution:
string | string[]String or strings to show in an AttributionControl. Only applicable ifattributionControloption is set totrue. -
logoPosition (Default:
bottom-left):stringSet the position of the mapbox logo. Possible values:top-lefttop-rightbottom-rightbottom-left
-
renderWorldCopies (Default:
true):booleanIftrue, multiple copies of the world will be rendered, when zoomed out. -
dragRotate (Default:
true):booleanSet tofalseto disable drag rotation, see mapbox DragRotateHandler -
pitchWithRotate (Default:
true):booleanSet tofalseto disable pitch with rotation, see mapbox PitchWithRotate -
trackResize (Default:
true):booleanIftrue, the map will automatically resize when the browser window resizes. -
touchZoomRotate (Default:
true):booleanIftrue, the "pinch to rotate and zoom" interaction is enabled. An Object value is passed as an option to TouchZoomRotateHandler#enable. -
doubleClickZoom (Default:
true):booleanIftrue, the "double click to zoom" interaction is enabled (see DoubleClickZoomHandler). -
keyboard (Default:
true):booleanIftrue, keyboard shortcuts are enabled (see KeyboardHandler). -
dragPan (Default:
true):booleanIftrue, the "drag to pan" interaction is enabled (see DragPanHandler). -
refreshExpiredTiles (Default:
true):booleanIffalse, the map won't attempt to re-request tiles once they expire per their HTTP cacheControl / expires headers. -
failIfMajorPerformanceCaveat (Default:
false):booleanIftrue, 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). -
bearingSnap (Default:
7):numberThe threshold, measured in degrees, that determines when the map's bearing (rotation) will snap to the north. For example, with a bearingSnap of 7, if the user rotates the map within 7 degrees of the north, the map will automatically snap to the exact north.
- userLocation locate the location of user, this props is active when is written as props.
-
style (required) :
string | objectMapbox map style, if changed, the style will be updated usingsetStyle. -
center (Default:
[ -0.2416815, 51.5285582 ]):[number, number]Center the map at the position at initialisation- Must be in longitude, latitude coordinate order (as opposed to latitude, longitude) to match GeoJSON (source: https://www.mapbox.com/mapbox-gl-js/api/#lnglat).
- Re-center the map if the value change regarding the prev value or the actual center position flyTo
-
zoom (Default:
[11]):[number]Zoom level of the map at initialisation wrapped in an array.- Check for reference equality between the previous value of zoom and the new one in order to update it or not.
-
maxBounds :
LngLatBounds | number[][]If set, the map is constrained to the given bounds [SouthWest, NorthEast] -
fitBounds :
[[number, number], [number, number]]If set, the map will center on the given coordinates, fitBounds -
fitBoundsOptions :
objectOptions for fitBounds -
bearing:
[number]Bearing (rotation) of the map at initialisation measured in degrees counter-clockwise from north.- Check the previous value and the new one, if the value changed update the bearing value flyTo
-
pitch:
[number]Pitch (tilt) of the map at initialisation, range :0 - 60 -
containerStyle :
objectThe style of the container of the map passed as an object -
className :
stringClassName passed down to the container div -
movingMethod (Default:
flyTo):stringdefine the method used when changing the center or zoom position. Possible value :jumpToeaseToflyTo
-
animationOptions :
objectOptions for moving animation see -
flyToOptions :
objectOptions for flyTo animation see
All mapir map events are implemented in this library, see events section on mapbox documentation api. All the events have the following signature (map: Mapbox.Map, evt: React.SyntheticEvent<any>) => void.
The event names are written in camelCase:
const events = {
// Triggered when style of the map has loaded
onStyleLoad,
// mapbox-gl events
onResize: 'resize',
onDblClick: 'dblclick',
onClick: 'click',
onMouseMove: 'mousemove',
onMouseOut: 'mouseout',
onMoveStart: 'movestart',
onMove: 'move',
onMoveEnd: 'moveend',
onMouseUp: 'mouseup',
onMouseDown: 'mousedown',
onDragStart: 'dragstart',
onDrag: 'drag',
onDragEnd: 'dragend',
onZoomStart: 'zoomstart',
onZoom: 'zoom',
onZoomEnd: 'zoomend',
onPitch: 'pitch',
onPitchStart: 'pitchstart',
onPitchEnd: 'pitchend',
onWebGlContextLost: 'webglcontextlost',
onWebGlContextRestored: 'webglcontextrestored',
onRemove: 'remove',
onContextMenu: 'contextmenu',
onRender: 'render',
onError: 'error',
onSourceData: 'sourcedata',
onDataLoading: 'dataloading',
onStyleDataLoading: 'styledataloading',
onStyleImageMissing: 'styleimagemissing',
onTouchCancel: 'touchcancel',
onData: 'data',
onSourceDataLoading: 'sourcedataloading',
onTouchMove: 'touchmove',
onTouchEnd: 'touchend',
onTouchStart: 'touchstart',
onStyleData: 'styledata',
onBoxZoomStart: 'boxzoomstart',
onBoxZoomEnd: 'boxzoomend',
onBoxZoomCancel: 'boxzoomcancel',
onRotateStart: 'rotatestart',
onRotate: 'rotate',
onRotateEnd: 'rotateend'
};Creates a new Mapbox layer and creates all the sources depending on the child Feature components. The layer also creates a source if no sourceId is passed.
If you change the value of the paint or the layout property of the layer, it will automatically update this property using setPaintProperty or setLayoutProperty respectively.
Only works with the first depth of the object.
import Mapir from "mapir-react-component";
...
<Mapir.Layer
type="symbol"
layout={{ "icon-image": "harbor-15" }}>
</Mapir.Layer>-
id :
stringThe id of the layer or generate an incremented number as id -
type (Default:
symbol) :stringThe type of the features children elements, possible values :-
symbol, Include a Mapboxsymbol(PointGeoJson) -
line, Include a Mapboxline(LineStringGeoJson) -
fill, Include a Mapboxpolygon(FillGeoJson) -
circle, Include a Mapboxcircle(PointGeoJson) -
raster, Include a Mapbox raster layer -
fill-extrusion, Include a Mapbox fill extrusion layer -
background, Include a Mapbox background layer -
heatmap, Include a Mapbox heatmap layer
-
-
layout:
objectMapbox layout object passed down to mapboxmap.addLayermethod mapbox layout api -
paint:
objectMapbox paint object passed down to mapboxmap.addLayermethod mapbox paint api -
geoJSONSourceOptions:
objectSource options merged to object passed tomap.addSource -
metadata:
objectmetadata parameter passed tomap.addLayer -
sourceLayer:
stringsource-layer parameter passed tomap.addLayer -
minZoom:
numberminzoom parameter passed tomap.addLayer -
maxZoom:
numbermaxzoom parameter passed tomap.addLayer -
filter:
any[]filter parameter passed tomap.addLayer, see how to use expressions to filter elements -
sourceId:
stringWhen passed to the layer, the source will not be created but only the layer and it will use the given source id. -
before:
stringPass the id of a layer, it will display the current layer before the layer defined by the id. mapbox api -
images:
[imageKey: string, image: HTMLImageElement, options: object]Also accepts array of the previous image tuple. Add images for use in layout with propicon-image. The value should be theimageKeystring of the tuple. Alternatively, use mapbox studio to upload the image, it will be fetched with the map style object. (see map.addImage options for the tuple options). -
onMouseMove
functionMouse move handler. mapbox map mouse event -
onMouseEnter
functionMouse enter handler. mapbox map mouse event -
onMouseLeave
functionMouse leave handler. mapbox map mouse event -
onMouseDown
functionMouse down handler. mapbox map mouse event -
onMouseUp
functionMouse up handler. mapbox map mouse event -
onClick
functionMouse click handler. mapbox map mouse event
Add a source to the map (for layers to use, for example).
import Mapir from "mapir-react-component";
...
const RASTER_SOURCE_OPTIONS = {
"type": "raster",
"tiles": [
"https://someurl.com/512/{z}/{x}/{y}",
],
"tileSize": 512
};
...
<Mapir.Source id="source_id" tileJsonSource={RASTER_SOURCE_OPTIONS} />
<Mapir.Layer type="raster" id="layer_id" sourceId="source_id" />-
id (required):
string -
geoJsonSource :
objectGeoJson source, see mapbox-gl GeoJson for options -
tileJsonSource :
objectTileJson source, see mapbox-gl TileJson for options -
onSourceAdded :
functionExecuted once the source is added to the map, the source is passed as a first argument. -
onSourceLoaded :
functionExecuted once the source data has been loaded for the first time (after mapbox-gl map.event:load), the source is passed as a first argument.
Add a Mapir raster to the map (instead of vector layers).
import Mapir from "mapir-react-component";
...
<Mapir.Raster id="source_id" apiKey={'Your_API_Key'} />
<Mapir.Layer type="raster" id="layer_id" sourceId="source_id" />-
id (required):
string -
onSourceAdded :
functionExecuted once the source is added to the map, the source is passed as a first argument. -
onSourceLoaded :
functionExecuted once the source data has been loaded for the first time (after mapbox-gl map.event:load), the source is passed as a first argument.
Display on the map all the information contained in a geoJSON file.
Note: GeoJSONLayer will not render any layers (line, circle, fill, etc...)
unless an associated [layer]Layout or [layer]Paint prop is provided.
import Mapir from "mapir-react-component";
...
<Mapir.GeoJSONLayer
data={geojson}
symbolLayout={{
"text-field": "{place}",
"text-font": ["Open Sans Semibold", "Arial Unicode MS Bold"],
"text-offset": [0, 0.6],
"text-anchor": "top"
}}/>-
data (required):
string | objectThe URL to the geojson file or the geojson file itself. -
lineLayout | symbolLayout | circleLayout | fillLayout | fillExtrusionLayout:
ObjectLayer layout information. mapbox layout api -
linePaint | symbolPaint | circlePaint | fillPaint | fillExtrusionPaint:
ObjectPaint information. mapbox paint api -
lineOnMouseDown | symbolOnMouseDown | circleOnMouseDown | fillOnMouseDown | fillExtrusionOnMouseDown:
functionMouse down handler. mapbox map mouse event -
lineOnMouseUp | symbolOnMouseUp | circleOnMouseUp | fillOnMouseUp | fillExtrusionOnMouseUp:
functionMouse up handler. mapbox map mouse event -
lineOnMouseMove | symbolOnMouseMove | circleOnMouseMove | fillOnMouseMove | fillExtrusionOnMouseMove:
functionMouse move handler. mapbox map mouse event -
lineOnMouseEnter | symbolOnMouseEnter | circleOnMouseEnter | fillOnMouseEnter | fillExtrusionOnMouseEnter:
functionMouse enter handler. mapbox map mouse event -
lineOnMouseLeave | symbolOnMouseLeave | circleOnMouseLeave | fillOnMouseLeave | fillExtrusionOnMouseLeave:
functionMouse leave handler. mapbox map api -
lineOnClick | symbolOnClick | circleOnClick | fillOnClick | fillExtrusionOnClick:
functionMouse click handler. mapbox map mouse event -
sourceOptions:
objectOptions object merged to the object used when callingGeoJSONSourcemethod -
layerOptions:
objectPassed down to the layer object when setting it out. -
before:
stringPass the id of a layer, it will display the current layer before the layer defined by the id. mapbox api
Displays a feature on the map. Can only be used when wrapped in a Layer component. The type of the feature is defined at the Layer level. If you want to create a new type, create an associated new layer.
import Mapir from "mapir-react-component";
...
<Mapir.Feature coordinates={[-0.13235092163085938,51.518250335096376]}/>-
coordinates (required) :
number[]Display the feature at the given position. -
properties :
objectProperties object passed down to the feature at the creation of the source. -
onClick :
(mapWithEvt: object) => voidTriggered when user click on the feature- Args contain the feature object, the map object and the arguments passed by mapbox from the event
click
- Args contain the feature object, the map object and the arguments passed by mapbox from the event
-
onMouseEnter :
(mapWithEvt: object) => voidTriggered when the mouse enter the feature element- Args contain the feature object, the map object and the arguments passed by mapbox from the event
onmousemove
- Args contain the feature object, the map object and the arguments passed by mapbox from the event
-
onMouseLeave :
(mapWithEvt: object) => voidTriggered when the mouse leave the feature element- Args contain the map object and the arguments passed by Mapbox from the event
onmousemove
- Args contain the map object and the arguments passed by Mapbox from the event
-
draggable (Default:
false) :booleanDefine wether the feature is draggable or not. -
onDragStart :
(mapWithEvt: object) => voidTriggered when the user start dragging the feature. -
onDrag :
(mapWithEvt: object) => voidTriggered when the user continue dragging the feature (ie. move). -
onDragEnd :
(mapWithEvt: object) => voidTriggered when the user stop dragging the feature.
Adds to the map an image that can be used as icon-image
Load local image. see docs
<Mapir.Image id={'image-uid'} data={someImage} />Load image from url. see docs
<Mapir.Image id={'image-uid'} url={'url/to/image'} />-
id (required):
stringthe image name -
url
stringA url to load the image from see docs -
data
ImageDataTypeThe image data see docs -
options
ImageOptionsTypeThe image options see docs -
onLoaded
() => voidWill be called when image loaded to map -
onError
(error: Error) => voidWill be called if image did not load
A custom react zoom control component.
import Mapir from "mapir-react-component";
...
<Mapir.ZoomControl/>-
onControlClick :
(map: object, zoomDiff: number) => voidtriggered when user clicks on minus or plus button -
style :
objectStyle object merged with internal style into the container -
className:
stringCustom style using className for the container -
zoomDiff :
numberThe shift number passed to the callbackonControlClick -
position (Default:
top-right):stringThe control position, Possible values :top-righttop-leftbottom-rightbottom-left
-
tabIndex :
numberdefine the tab index value of the top container tag
A custom react scale feedback control component.
import { ScaleControl } from "react-mapbox-gl";
...
<Mapir.ScaleControl/>-
measurement (Default:
km):string, Possible values:kmmi
-
style:
objectStyle object merged with internal style into the container -
className:
stringCustom style using className for the container -
position (Default:
bottom-right):stringThe control position, Possible values :top-righttop-leftbottom-rightbottom-left
-
tabIndex :
numberdefine the tab index value of the top container tag
Displays the current map rotation, also resets the rotation to its original value on click.
import Mapir from "mapir-react-component";
...
<Mapir.RotationControl/>-
style :
objectStyle object merged with internal style into the container -
className :
stringGet passed to the container div -
position (Default:
top-right):stringThe control position, Possible values :top-righttop-leftbottom-rightbottom-left
-
tabIndex :
numberdefine the tab index value of the top container tag
The popup component allows you to display a popup tooltip on top of the map using mapbox-gl-js.
import Mapir from "mapir-react-component";
...
<Mapir.Popup
coordinates={[-0.13235092163085938,51.518250335096376]}
offset={{
'bottom-left': [12, -38], 'bottom': [0, -38], 'bottom-right': [-12, -38]
}}>
<h1>Popup</h1>
</Mapir.Popup>-
coordinates (required):
[number, number]Display the popup at the given position. -
anchor:
stringSet the anchor point of the popup, Possible values:topbottomleftrightcentertop-lefttop-rightbottom-leftbottom-right-
null | undefined: When not set, the anchor is automatically calculated to keep the content of the popup visible.
-
offset (Default: 0):
number | number[] | objectSet the offset of popup, where the tip of the popup will be pointing.- When
numberis passed, the popup will be offset by that number for all anchor positions. - When an
number[]is passed (e.g. [-12, 30]), the popup will be offset by that point. - When
objectis passed, it must contain keys for different anchor positions and values as the offset (numberornumber[])
- When
-
onClick:
functionTriggered whenever user click on the popup -
style:
objectApply style to the marker container -
className:
stringApply the className to the container of the popup -
tabIndex :
numberdefine the tab index value of the top container tag
Add an HTML marker to the map.
Note: When rendering many objects, avoid using Markers as it will negatively affect performance. Use Layers and Features instead.
import Mapir from "mapir-react-component";
...
<Mapir.Marker
coordinates={[-0.2416815, 51.5285582]}
anchor="bottom"
Image={markerUrl}>
</Mapir.Marker>-
coordinates (required):
[number, number]Display the marker at the given position. -
anchor:
stringSame as Popup's anchor property. -
offset:
stringSame as Popup's offset property. -
onClick:
functionTriggered whenever user click on the marker -
style:
objectApply style to the marker container -
className:
stringApply the className to the container of the Marker -
tabIndex :
numberdefine the tab index value of the top container tag
Create a cluster of Marker.
import Mapir from "mapir-react-component";
...
clusterMarker = (coordinates) => (
<Mapir.Marker coordinates={coordinates} style={styles.clusterMarker}>
C
</Mapir.Marker>
);
...
<Mapir.Cluster ClusterMarkerFactory={this.clusterMarker}>
{
places.features.map((feature, key) =>
<Mapir.Marker
key={key}
style={styles.marker}
coordinates={feature.geometry.coordinates}
onClick={this.onMarkerClick.bind(this, feature.geometry.coordinates)}>
M
</Mapir.Marker>
)
}
</Mapir.Cluster>-
ClusterMarkerFactory (required):
(coordinates: number[], pointCount: number, getLeaves: (limit?: number, offset?: number) => Array<React.ReactElement<any>>) => MarkerA function called for every cluster, the function must return a Marker component-
getLeaves()returnClusterchildren of a cluster, with pagination support: the limit is the number of points to return (set to Infinity for all points, default to 10), and offset is the number of points to skip (for pagination).
-
-
radius: Default: 60:
numberCluster radius, in pixels. -
minZoom: Default: 0:
numberMinimum zoom level at which clusters are generated. -
maxZoom: Default: 16:
numberMaximum zoom level at which clusters are generated. -
extent: Default: 512:
number(Tiles) Tile extent. Radius is calculated relative to this value. -
nodeSize: Default: 64:
numberSize of the KD-tree leaf node. Affects performance. -
log: Default: false:
booleanWhether timing info should be logged. -
zoomOnClick: Default: false:
booleanZoom to bounds of the cluster on click. -
zoomOnClickPadding: Default: 20:
numberThe amount of padding in pixels to add to the cluster bounds for the zoom. -
style:
objectApply style to the marker container -
className:
stringApply the className to the container of the Marker -
tabIndex :
numberdefine the tab index value of the top container tag
import Mapir from "mapir-react-component";
...
<Mapir.DrawControl />