React Simple Maps

3.0.0 · active · verified Sun Apr 19

React Simple Maps is a component library designed to facilitate the creation of interactive SVG maps within React applications. As of version 3.0.0, released in July 2022, it provides a declarative API built on top of `d3-geo` and `topojson-client`, specifically handling common complexities like panning, zooming, and rendering optimizations. Unlike many mapping solutions, it does not bundle map data; users are required to provide their own GeoJSON or TopoJSON files. This approach makes the library lean and flexible, allowing seamless integration with other React ecosystem tools such as `react-spring` or `react-annotation`. While its average release cycle was historically around 145 days, v3.0.0 has been the latest stable release for some time, suggesting a slower development cadence or maintenance mode. Its core differentiator lies in its minimalist design, offering building blocks for custom map charts without unnecessary bloat, focusing on the core task of rendering and interacting with geographic data.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart renders a basic world map using the `geoEqualEarth` projection, fetching a TopoJSON file from a URL. It then overlays a set of predefined markers with labels for several South American capitals. The map is set to fill 100% width and auto height.

import React from 'react';
import ReactDOM from 'react-dom/client';
import { ComposableMap, Geographies, Geography, Marker } from 'react-simple-maps';

const geoUrl = 'https://raw.githubusercontent.com/deldersveld/topojson/master/world-countries.json';

const markers = [
  { markerOffset: -15, name: 'Buenos Aires', coordinates: [-58.3816, -34.6037] },
  { markerOffset: -15, name: 'La Paz', coordinates: [-68.1193, -16.4897] },
  { markerOffset: 25, name: 'Brasília', coordinates: [-47.8825, -15.7942] },
  { markerOffset: 25, name: 'Santiago', coordinates: [-70.6693, -33.4489] },
  { markerOffset: -15, name: 'Bogotá', coordinates: [-74.0721, 4.7110] },
  { markerOffset: 25, name: 'Quito', coordinates: [-78.4678, -0.1807] }
];

const MapChart = () => {
  return (
    <ComposableMap
      projection="geoEqualEarth"
      projectionConfig={{
        scale: 160
      }}
      style={{
        width: '100%',
        height: 'auto'
      }}
    >
      <Geographies geography={geoUrl}>
        {({ geographies }) =>
          geographies.map((geo) => (
            <Geography
              key={geo.rsmKey}
              geography={geo}
              fill="#D6D6DA"
              stroke="#FFFFFF"
              strokeWidth={0.5}
            />
          ))
        }
      </Geographies>
      {markers.map(({ name, coordinates, markerOffset }) => (
        <Marker key={name} coordinates={coordinates}>
          <circle r={8} fill="#F00" stroke="#fff" strokeWidth={2} />
          <text
            textAnchor="middle"
            y={markerOffset}
            style={{ fontFamily: 'system-ui', fill: '#5D5A6D', fontSize: '10px' }}
          >
            {name}
          </text>
        </Marker>
      ))}
    </ComposableMap>
  );
};

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<MapChart />);

view raw JSON →