Search results

Getting started with React Maps component

05 Oct 2022 / 9 minutes to read

This section explains you the steps required to create a map and demonstrate the basic usage of the maps component.

You can explore some useful features in the Maps component using the following video.

Dependencies

Below is the list of minimum dependencies required to use the Maps.

Copied to clipboard
|-- @syncfusion/ej2-react-maps
    |-- @syncfusion/ej2-maps
    |-- @syncfusion/ej2-base
    |-- @syncfusion/ej2-svg-base
    |-- @syncfusion/ej2-data
    |-- @syncfusion/ej2-pdf-export
|-- @syncfusion/ej2-react-base

Installation and Configuration

To get started with the React application, create-react-app can be used to setup the application. To install create-react-app run the following command.

Copied to clipboard
npm install -g create-react-app

To create basic React application, run the following command.

Copied to clipboard
create-react-app quickstart

Now, the application is created in the quickstart folder. Run the following command one-by-one to navigate to the quickstart folder, and install the required npm packages.

Copied to clipboard
cd quickstart

npm install

In the quickstart application, the Syncfusion component is added in the JavaScript file.

Creating a React application with TypeScript

To create React application with TypeScript, use the following command.

Copied to clipboard
create-react-app quickstart --template typescript

Now, the application is created in the quickstart folder. Run the following command one-by-one to navigate to the quickstart folder, and install the required npm packages.

Copied to clipboard
cd quickstart

npm install

Adding Syncfusion packages

All the available Essential JS 2 packages are published in npmjs.com public registry. To install Maps package, use the following command.

Copied to clipboard
npm install @syncfusion/ej2-react-maps --save

Add Map to the Project

Now, the Maps component can be added in the application. To initialize the Maps component in the React application, import the Maps component in the src/App.js or src/App.tsx as per the application. Please use the below code to include the Maps component in the application.

Copied to clipboard
import React from 'react';
import { MapsComponent } from '@syncfusion/ej2-react-maps';

function App() {
   return (<MapsComponent id="container" />);
}

export default App;

As we didn’t specify shapeData to the maps, no shape will be rendered and only an empty SVG element is appended to the maps container.

Run the application

The Maps component is now included in the quickstart application. Use the following command to run the application.

Copied to clipboard
npm start

Module Injection

Maps component are segregated into individual feature-wise modules. In order to use a particular feature, you need to inject its feature services using Inject tag. Find the modules available in maps and its description as follows.

  • Annotations - Inject this provider to use annotations feature.
  • Bubble - Inject this provider to use bubble feature.
  • DataLabel - Inject this provider to use data label feature.
  • Highlight - Inject this provider to use highlight feature.
  • Legend - Inject this provider to use legend feature.
  • Marker - Inject this provider to use marker feature.
  • MapsTooltip - Inject this provider to use tooltip series.
  • NavigationLine - Inject this provider to use navigation lines feature.
  • Selection - Inject this provider to use selection feature.
  • Zoom - Inject this provider to use zooming and panning feature.

For this application we are going to use tooltip, data label and legend features of the maps. Now import the MapsTooltip, DataLabel and Legend modules from maps package and inject it into the Maps component using Inject tag with required services.

Copied to clipboard
import { Maps, Legend, DataLabel, MapsTooltip } from '@syncfusion/ej2-maps';
import * as React from 'react';
import { MapsComponent } from '@syncfusion/ej2-react-maps';

class App extends React.Component {
render() {
   return (
<MapsComponent id="element">
    <Inject services={[DataLabel, Legend, MapsTooltip]} />
</MapsComponent>);
  }
}
export default App;
Copied to clipboard
import { Legend, DataLabel, MapsTooltip } from '@syncfusion/ej2-maps';
import * as React from 'react';
import { MapsComponent } from '@syncfusion/ej2-react-maps';
class App extends React.Component {
    render() {
        return (<MapsComponent id="element">
    <Inject services={[DataLabel, Legend, MapsTooltip]}/>
</MapsComponent>);
    }
}
export default App;

Render shapes from GeoJSON data

This section explains how to bind GeoJSON data to the map.

Copied to clipboard
let usMap: Object =
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
    { "type": "Feature", "properties": { "iso_3166_2": "MA", "name": "Massachusetts", "admin": "United States of America" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ -70.801756294617277, 41.248076234530558 ]] ] ] }
    }
]
//..
};
Copied to clipboard
let usMap = {
    "type": "FeatureCollection",
    "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
    "features": [
        { "type": "Feature", "properties": { "iso_3166_2": "MA", "name": "Massachusetts", "admin": "United States of America" }, "geometry": { "type": "MultiPolygon", "coordinates": [[[[-70.801756294617277, 41.248076234530558]]]] }
        }
    ]
    //..
};

Elements in the maps will get rendered in the layers. So add a layer collection to the maps by using layers property. Now bind the GeoJSON data to the shapeData property.

Source
Preview
index.jsx
index.tsx
Copied to clipboard
import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective } from '@syncfusion/ej2-react-maps';
ReactDOM.render(<MapsComponent id="maps">
            <LayersDirective>
                <LayerDirective shapeData={world_map}>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent>, document.getElementById("maps"));
Copied to clipboard
import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective } from '@syncfusion/ej2-react-maps';

ReactDOM.render(
        <MapsComponent id="maps">
            <LayersDirective>
                <LayerDirective shapeData={world_map}>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent>,
document.getElementById("maps") as HTMLElement
);

Bind data source to map

The following properties in layers are used for binding data source to map.

  • [dataSource]
  • [shapeDataPath]
  • [shapePropertyPath]

The dataSource property takes collection value as input. For example, the list of objects can be provided as input. This data is further used in tooltip, data label, bubble, legend and in color mapping.

The shapeDataPath property used to refer the data ID in dataSource. Where as, the shapePropertyPath property is used to refer the column name in shapeData to identify the shape. Both the properties are related to each other. When the values of the shapeDataPath property in the dataSource property and the value of shapePropertyPath in the shapeData property match, then the associated object from the dataSource is bound to the corresponding shape.

The JSON object “electionData” is used as data source below.

Source
Preview
index.jsx
index.tsx
Copied to clipboard
import { world_map } from 'world-map.ts';
import { uncountries } from 'data.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective } from '@syncfusion/ej2-react-maps';
ReactDOM.render(<MapsComponent id="maps">
            <LayersDirective>
                <LayerDirective shapeData={world_map} shapeDataPath='Country' shapePropertyPath='name' dataSource={uncountries}>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent>, document.getElementById("maps"));
Copied to clipboard
import { world_map } from 'world-map.ts';
import { uncountries } from 'data.ts'
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective } from '@syncfusion/ej2-react-maps';

ReactDOM.render(
        <MapsComponent id="maps">
            <LayersDirective>
                <LayerDirective shapeData={world_map} shapeDataPath='Country' shapePropertyPath='name' dataSource={uncountries}>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent>,
document.getElementById("maps") as HTMLElement
);

Note: Refer the value of the data source of world-map.ts and data.ts here.

Apply Color Mapping

The Color Mapping feature supports customization of shape colors based on the underlying value of shape received from bounded data. Specify the field name from which the values have to be compared for the shapes in colorValuePath property in shapeSettings.

Specify color and value in colorValuePath property. Here ‘#D84444’ is specified for ‘Trump’ and ‘#316DB5’ is specified for ‘Clinton’.

Source
Preview
index.jsx
index.tsx
Copied to clipboard
import { world_map } from 'world-map.ts';
import { uncountries } from 'data.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective } from '@syncfusion/ej2-react-maps';
ReactDOM.render(<MapsComponent id="maps">
            <LayersDirective>
                <LayerDirective shapeData={world_map} shapeDataPath='Country' shapePropertyPath='name' dataSource={uncountries} shapeSettings={{
    colorValuePath: 'Membership',
    colorMapping: [
        {
            value: 'Permanent', color: '#D84444'
        },
        {
            value: 'Non-Permanent', color: '#316DB5'
        }
    ]
}}>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent>, document.getElementById("maps"));
Copied to clipboard
import { world_map } from 'world-map.ts';
import { uncountries } from 'data.ts'
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective } from '@syncfusion/ej2-react-maps';

ReactDOM.render(
        <MapsComponent id="maps">
            <LayersDirective>
                <LayerDirective shapeData={world_map} shapeDataPath='Country' shapePropertyPath='name' dataSource={uncountries}
                    shapeSettings={ {
                        colorValuePath: 'Membership',
                        colorMapping: [
                            {
                                value: 'Permanent', color: '#D84444'
                            },
                            {
                                value: 'Non-Permanent', color: '#316DB5'
                            }]
                    } }>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent>,
document.getElementById("maps") as HTMLElement
);

Note: Refer the value of the data source of world-map.ts and data.ts here.

Add Title for Maps

You can add a title using titleSettings property to the map to provide quick information to the user about the shapes rendered in the map.

Source
Preview
index.jsx
index.tsx
Copied to clipboard
import { world_map } from 'world-map.ts';
import { uncountries } from 'data.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective } from '@syncfusion/ej2-react-maps';
ReactDOM.render(<MapsComponent id="maps" titleSettings={{ text: 'World map with membership' }}>
            <LayersDirective>
                <LayerDirective shapeData={world_map} shapeDataPath='Country' shapePropertyPath='name' dataSource={uncountries} shapeSettings={{
    colorValuePath: 'Membership',
    colorMapping: [
        {
            value: 'Permanent', color: '#D84444'
        },
        {
            value: 'Non-Permanent', color: '#316DB5'
        }
    ]
}}>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent>, document.getElementById("maps"));
Copied to clipboard
import { world_map } from 'world-map.ts';
import { uncountries } from 'data.ts'
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective } from '@syncfusion/ej2-react-maps';

ReactDOM.render(
        <MapsComponent id="maps"  titleSettings={ { text: 'World map with membership' } }>
            <LayersDirective>
                <LayerDirective shapeData={world_map} shapeDataPath='Country' shapePropertyPath='name' dataSource={uncountries}
                    shapeSettings={ {
                        colorValuePath: 'Membership',
                        colorMapping: [
                            {
                               value: 'Permanent', color: '#D84444'
                           },
                           {
                               value: 'Non-Permanent', color: '#316DB5'
                           }]
                    } }>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent>,
document.getElementById("maps") as HTMLElement
);

Note: Refer the value of the data source of world-map.ts and data.ts here.

Enable Legend

You can show legend for the maps by setting true to the visibleproperty in legendSettings object and by injecting the Legend service using Inject tag.

Source
Preview
index.jsx
index.tsx
Copied to clipboard
import { world_map } from 'world-map.ts';
import { uncountries } from 'data.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective, Inject, Legend } from '@syncfusion/ej2-react-maps';
ReactDOM.render(<MapsComponent id="maps" legendSettings={{ visible: true }}>
        <Inject services={[Legend]}/>
            <LayersDirective>
                <LayerDirective shapeData={world_map} shapeDataPath='Country' shapePropertyPath='name' dataSource={uncountries} shapeSettings={{
    colorValuePath: 'Membership',
    colorMapping: [
        {
            value: 'Permanent', color: '#D84444'
        },
        {
            value: 'Non-Permanent', color: '#316DB5'
        }
    ]
}}>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent>, document.getElementById("maps"));
Copied to clipboard
import { world_map } from 'world-map.ts';
import { uncountries } from 'data.ts'
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective, Inject, Legend } from '@syncfusion/ej2-react-maps';

ReactDOM.render(
        <MapsComponent id="maps" legendSettings={ { visible: true } } >
        <Inject services={[Legend]} />
            <LayersDirective>
                <LayerDirective shapeData={world_map} shapeDataPath='Country' shapePropertyPath='name' dataSource={uncountries}
                    shapeSettings={ {
                        colorValuePath: 'Membership',
                        colorMapping: [
                            {
                                value: 'Permanent', color: '#D84444'
                            },
                            {
                                value: 'Non-Permanent', color: '#316DB5'
                            }]
                    } }>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent>,
document.getElementById("maps") as HTMLElement
);

Note: Refer the value of the data source of world-map.ts and data.ts here.

Add Data Label

You can add data labels to show additional information of the shapes in map. This can be achieved by setting visible property to true in the dataLabelSettings object and by injecting DataLabel service using Inject tag.

Source
Preview
index.jsx
index.tsx
Copied to clipboard
import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective, Inject, DataLabel } from '@syncfusion/ej2-react-maps';
ReactDOM.render(<MapsComponent id="maps">
        <Inject services={[DataLabel]}/>
            <LayersDirective>
                <LayerDirective shapeData={world_map} dataLabelSettings={{
    visible: true,
    labelPath: 'name',
    smartLabelMode: 'Trim'
}}>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent>, document.getElementById("maps"));
Copied to clipboard
import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective, Inject, DataLabel } from '@syncfusion/ej2-react-maps';

ReactDOM.render(
        <MapsComponent id="maps">
        <Inject services={[DataLabel]} />
            <LayersDirective>
                <LayerDirective shapeData={world_map}
                dataLabelSettings={ {
                    visible: true,
                    labelPath: 'name',
                    smartLabelMode: 'Trim'
                } }>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent>,
document.getElementById("maps") as HTMLElement
);

Enable Tooltip

The tooltip is useful when you cannot display information by using the data labels due to space constraints. You can enable tooltip by setting the visible property as true in tooltipSettings object and by injecting MapsTooltip service using Inject tag.

Source
Preview
index.jsx
index.tsx
Copied to clipboard
import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective, Inject, MapsTooltip } from '@syncfusion/ej2-react-maps';
ReactDOM.render(<MapsComponent id="maps">
        <Inject services={[MapsTooltip]}/>
            <LayersDirective>
                <LayerDirective shapeData={world_map} tooltipSettings={{
    visible: true,
    valuePath: 'name'
}}>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent>, document.getElementById("maps"));
Copied to clipboard
import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective, Inject, MapsTooltip } from '@syncfusion/ej2-react-maps';

ReactDOM.render(
        <MapsComponent id="maps">
        <Inject services={[MapsTooltip]} />
            <LayersDirective>
                <LayerDirective shapeData={world_map}
                 tooltipSettings={ {
                    visible: true,
                    valuePath: 'name'
                } }>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent>,
document.getElementById("maps") as HTMLElement
);