1 | <img src="https://img.shields.io/bundlephobia/minzip/react-simple-maps?color=%2328cb95&label=gzip" />
|
2 |
|
3 | # react-simple-maps
|
4 |
|
5 | Create beautiful SVG maps in react with d3-geo and topojson using a declarative api.
|
6 |
|
7 | Read the [docs](https://www.react-simple-maps.io/docs/getting-started/), or check out the [examples](https://www.react-simple-maps.io/examples/).
|
8 |
|
9 | ### Why
|
10 |
|
11 | `React-simple-maps` aims to make working with svg maps in react easier. It handles tasks such as panning, zooming and simple rendering optimization, and takes advantage of parts of [d3-geo](https://github.com/d3/d3-geo) and topojson-client instead of relying on the entire d3 library.
|
12 |
|
13 | Since `react-simple-maps` leaves DOM work to react, it can also easily be used with other libraries, such as [react-spring](https://github.com/react-spring/react-spring) and [react-annotation](https://github.com/susielu/react-annotation/).
|
14 |
|
15 | ### Install
|
16 |
|
17 | To install `react-simple-maps`
|
18 |
|
19 | ```bash
|
20 | $ npm install --save react-simple-maps
|
21 | ```
|
22 |
|
23 | ...or if you use yarn:
|
24 |
|
25 | ```bash
|
26 | $ yarn add react-simple-maps
|
27 | ```
|
28 |
|
29 | ### Usage
|
30 |
|
31 | `React-simple-maps` exposes a set of components that can be combined to create svg maps with markers and annotations. In order to render a map you have to provide a reference to a valid topojson file. You can find example topojson files on [here](https://github.com/topojson/world-atlas) or [here](https://github.com/deldersveld/topojson). To learn how to make your own topojson maps from shapefiles, please read ["How to convert and prepare TopoJSON files for interactive mapping with d3"](https://hackernoon.com/how-to-convert-and-prepare-topojson-files-for-interactive-mapping-with-d3-499cf0ced5f) on medium.
|
32 |
|
33 | ```jsx
|
34 | import React from "react";
|
35 | import ReactDOM from "react-dom";
|
36 | import { ComposableMap, Geographies, Geography } from "react-simple-maps";
|
37 |
|
38 | // url to a valid topojson file
|
39 | const geoUrl =
|
40 | "https://raw.githubusercontent.com/deldersveld/topojson/master/world-countries.json";
|
41 |
|
42 | const App = () => {
|
43 | return (
|
44 | <div>
|
45 | <ComposableMap>
|
46 | <Geographies geography={geoUrl}>
|
47 | {({ geographies }) =>
|
48 | geographies.map((geo) => (
|
49 | <Geography key={geo.rsmKey} geography={geo} />
|
50 | ))
|
51 | }
|
52 | </Geographies>
|
53 | </ComposableMap>
|
54 | </div>
|
55 | );
|
56 | };
|
57 |
|
58 | document.addEventListener("DOMContentLoaded", () => {
|
59 | ReactDOM.render(<App />, document.getElementById("app"));
|
60 | });
|
61 | ```
|
62 |
|
63 | Check out the [live example](https://codesandbox.io/s/basic-map-wvlol)
|
64 |
|
65 | The above will render a world map using the [equal earth projection](https://observablehq.com/@d3/equal-earth). You can read more about this projection on [Shaded Relief](http://shadedrelief.com/ee_proj/) and on [Wikipedia](https://en.wikipedia.org/wiki/Equal_Earth_projection).
|
66 |
|
67 | For other examples and components, check out the [documentation](https://www.react-simple-maps.io/docs/getting-started).
|
68 |
|
69 | ### Map files
|
70 |
|
71 | React-simple-maps does not restrict you to one specific map and relies on custom map files that you can modify in any way necessary for the project. This means that you can visualise countries, regions, and continents in various resolutions, as long as they can be represented using geojson/topojson.
|
72 |
|
73 | In order for this to work properly, you will however need to provide these valid map files to react-simple-maps yourself. Luckily, there are decent sources for map files on github and elsewhere. Here are some you can check out:
|
74 |
|
75 | * [Natural Earth](https://github.com/nvkelso/natural-earth-vector)
|
76 | * [Topojson maps by @deldersveld](https://github.com/deldersveld/topojson)
|
77 | * [Topojson world atlas](https://github.com/topojson/world-atlas)
|
78 |
|
79 | ### License
|
80 |
|
81 | MIT licensed. Copyright (c) Richard Zimerman 2017. See [LICENSE.md](https://github.com/zcreativelabs/react-simple-maps/blob/master/LICENSE) for more details.
|