Skip to content

Latest commit

 

History

History
147 lines (105 loc) · 3.25 KB

get-started.md

File metadata and controls

147 lines (105 loc) · 3.25 KB

Get Started

You may find complete project setups in get-started examples.

Installation

Using react-map-gl requires node >= 12 and react >= 16.3.

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';

npm install --save react-map-gl mapbox-gl @types/mapbox-gl
npm install --save react-map-gl maplibre-gl

Example

import * as React from 'react';
import Map from 'react-map-gl';

function App() {
  return (
    <Map
      mapboxAccessToken="<Mapbox access token>"
      initialViewState={{
        longitude: -122.4,
        latitude: 37.8,
        zoom: 14
      }}
      style={{width: 600, height: 400}}
      mapStyle="mapbox://styles/mapbox/streets-v9"
    />
  );
}

See about Mapbox tokens for alternatives to providing a Mapbox token.

import * as React from 'react';
import Map from 'react-map-gl/maplibre';

function App() {
  return (
    <Map
      initialViewState={{
        longitude: -122.4,
        latitude: 37.8,
        zoom: 14
      }}
      style={{width: 600, height: 400}}
      mapStyle="https://api.maptiler.com/maps/streets/style.json?key=get_your_own_key"
    />
  );
}

Styling

The base map library requires its stylesheet be included at all times. The marker, popup and navigation components in react-map-gl also need the stylesheet to work properly.

You may add the stylesheet to the head of your page:

<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v<YOUR_MAPBOX_VERSION>/mapbox-gl.css' rel='stylesheet' />

Find out your mapbox version by running yarn list mapbox-gl or npm ls mapbox-gl.

<link href='https://unpkg.com/maplibre-gl@<YOUR_MAPLIBRE_VERSION>/dist/maplibre-gl.css' rel='stylesheet' />

Find out your maplibre version by running yarn list maplibre-gl or npm ls maplibre-gl.

Or embed it in your app by using css-loader with Webpack or postcss with rollup:

import 'mapbox-gl/dist/mapbox-gl.css';
import 'maplibre-gl/dist/maplibre-gl.css';

Using with a Compatible Fork

npm install --save react-map-gl my-map-gl-fork

Then override the mapLib prop of Map:

import * as React from 'react';
import Map from 'react-map-gl';

// Include style sheet
import 'my-map-gl-fork/path/to/style-sheet.css';

function App() {
  return <Map mapLib={import('my-map-gl-fork')} />;
}