English | 日本語 | Español (Latinoamérica)
Mapbox GL JS provider for the MapConductor React SDK. Renders a Mapbox map through MapConductor's provider-independent camera, marker, and overlay API, so the same application code can also run on Google Maps, MapLibre, Leaflet, OpenLayers, ArcGIS, Cesium, or HERE.
npm install @mapconductor/react-for-mapbox@mapconductor/js-sdk-core and @mapconductor/js-sdk-react (used for markers and
other shared components) are installed automatically as dependencies. Your
code imports from both directly, so with pnpm's strict (isolated)
node_modules — or whenever you prefer to declare everything you import —
install them explicitly instead:
npm install @mapconductor/react-for-mapbox @mapconductor/js-sdk-core @mapconductor/js-sdk-reactmapbox-gl is bundled as a dependency. The mapbox:// style presets require an
access token from your Mapbox account; the
OSM-based presets work without one.
The simplest possible map app, built with MapConductor + Mapbox: click the marker and a "Hello, MapConductor" bubble pops up. You can build it in the 5 steps below. It uses the openstreetmap.jp styles, which need no access token, so you can copy-paste and it just works.
Create a React + TypeScript project with Vite.
npm create vite@latest hello-map -- --template react-ts
cd hello-map
npm install
npm run devInstall the package needed to show a map. We use Mapbox here, but you can use other map modules too.
npm install @mapconductor/react-for-mapbox@mapconductor/react-for-mapbox— components / hooks for Mapbox@mapconductor/js-sdk-react/@mapconductor/js-sdk-coreare installed automatically as dependencies.
Create the map state with useMapboxViewState and render it with
<MapBoxMapView2D>. Don't forget the style CSS import. Give the outer element a
height to make it full-screen.
import {
MapboxDesign,
MapBoxMapView2D,
useMapboxViewState,
} from '@mapconductor/react-for-mapbox';
import '@mapconductor/react-for-mapbox/style.css';
import { createGeoPoint, createMapCameraPosition } from '@mapconductor/js-sdk-core';
const TOKYO = createGeoPoint({ latitude: 35.6812, longitude: 139.7671 });
const INITIAL_CAMERA = createMapCameraPosition({ position: TOKYO, zoom: 14 });
export default function App() {
const mapViewState = useMapboxViewState({
mapDesignType: MapboxDesign.OsmBrightEn,
cameraPosition: INITIAL_CAMERA,
});
return (
<div style={{ width: '100vw', height: '100vh' }}>
<MapBoxMapView2D state={mapViewState} />
</div>
);
}Use MapBoxMapView instead of MapBoxMapView2D for the globe projection.
Create the marker state with createMarkerState and register it with
<Marker>. Write overlays as child elements of the map component.
import { useMemo } from 'react';
import { createMarkerState } from '@mapconductor/js-sdk-core';
import { Marker } from '@mapconductor/js-sdk-react';
// ...inside App...
const marker = useMemo(
() => createMarkerState({ id: 'hello', position: TOKYO }),
[],
);
// ...inside return...
<MapBoxMapView2D state={mapViewState}>
<Marker state={marker} />
</MapBoxMapView2D>Track the selected state with useState, set it to true in the marker's
onClick, and render <InfoBubble> only while selected. This is the finished
app.
import { useMemo, useState } from 'react';
import {
MapboxDesign,
MapBoxMapView2D,
useMapboxViewState,
} from '@mapconductor/react-for-mapbox';
import '@mapconductor/react-for-mapbox/style.css';
import {
createGeoPoint,
createMapCameraPosition,
createMarkerState,
} from '@mapconductor/js-sdk-core';
import { InfoBubble, Marker } from '@mapconductor/js-sdk-react';
const TOKYO = createGeoPoint({ latitude: 35.6812, longitude: 139.7671 });
const INITIAL_CAMERA = createMapCameraPosition({ position: TOKYO, zoom: 14 });
export default function App() {
const mapViewState = useMapboxViewState({
mapDesignType: MapboxDesign.OsmBrightEn,
cameraPosition: INITIAL_CAMERA,
});
const [selected, setSelected] = useState(false);
const marker = useMemo(
() => createMarkerState({
id: 'hello',
position: TOKYO,
onClick: () => setSelected(true),
}),
[],
);
return (
<div style={{ width: '100vw', height: '100vh' }}>
<MapBoxMapView2D state={mapViewState} onMapClick={() => setSelected(false)}>
<Marker state={marker} />
{selected && (
<InfoBubble marker={marker}>
<div style={{ padding: '8px 12px', fontWeight: 600 }}>
Hello, MapConductor
</div>
</InfoBubble>
)}
</MapBoxMapView2D>
</div>
);
}- Coordinates, cameras and markers are created with
js-sdk-corefunctions (provider-independent). - The map component and hooks come from
react-for-mapbox(provider-specific). - Write overlays as child elements of the map component.
- Control show / hide with React
useState.
@mapconductor/js-sdk-core— geometry, camera, and state primitives@mapconductor/js-sdk-react— sharedMarker,Markers, shapes, and info bubbles
