Getting Started

22 Nov 202324 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.

|-- @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.

     npm install -g create-react-app

To create basic React application, run the following command.

     create-react-app quickstart

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

    cd quickstart

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.

     create-react-app quickstart --template typescript

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

    cd quickstart

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.

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.

     import { usMap } from './usa-map';
     import React from 'react';
     import { MapsComponent, LayersDirective, LayerDirective } from '@syncfusion/ej2-react-maps';
     export function App() {
      return (<MapsComponent>
               <LayersDirective>
                  <LayerDirective shapeData={usMap}>
                  </LayerDirective>
               </LayersDirective>
         </MapsComponent>);
      }

    export default App;

Run the application

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

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. You can 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 feature.
  • 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 example, 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.

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

export function App() {
   return (<MapsComponent>
        <Inject services={[DataLabel, Legend, MapsTooltip]} />
    </MapsComponent>);
  }

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

Render shapes from GeoJSON data

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

      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 ]] ] ] }
         }
     ]
   };

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.

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';
export function App() {
    return (<MapsComponent id="maps">
            <LayersDirective>
                <LayerDirective shapeData={world_map}>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
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';
export function App() {
    return (<MapsComponent id="maps">
            <LayersDirective>
                <LayerDirective shapeData={world_map}>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);

Bind data source to map

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

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.

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';

export function App() {
   return(
            <MapsComponent >
                <LayersDirective>
                    <LayerDirective shapeData={world_map} shapeDataPath='Country' shapePropertyPath='name' dataSource={uncountries}>
                    </LayerDirective>
                </LayersDirective>
            </MapsComponent>
    );
}

const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
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';

export function App() {
   return(
            <MapsComponent >
                <LayersDirective>
                    <LayerDirective shapeData={world_map} shapeDataPath='Country' shapePropertyPath='name' dataSource={uncountries}>
                    </LayerDirective>
                </LayersDirective>
            </MapsComponent>
    );
}

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

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’.

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';

export function App() {
return(<MapsComponent >
                <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>
);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
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';

export function App() {
return(<MapsComponent >
                <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>
);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);

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.

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';

export function App() {
  return(<MapsComponent   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>
);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
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';

export function App() {
  return(<MapsComponent   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>
);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);

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.

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';

export function App() {
    return(<MapsComponent  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>
);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
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';

export function App() {
    return(<MapsComponent  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>
);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);

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.

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';

export function App(){
  return(<MapsComponent >
            <Inject services={[DataLabel]} />
                <LayersDirective>
                    <LayerDirective shapeData={world_map}
                    dataLabelSettings={ {
                        visible: true,
                        labelPath: 'name',
                        smartLabelMode: 'Trim'
                    } }>
                    </LayerDirective>
                </LayersDirective>
        </MapsComponent>
);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
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';

export function App(){
  return(<MapsComponent >
            <Inject services={[DataLabel]} />
                <LayersDirective>
                    <LayerDirective shapeData={world_map}
                    dataLabelSettings={ {
                        visible: true,
                        labelPath: 'name',
                        smartLabelMode: 'Trim'
                    } }>
                    </LayerDirective>
                </LayersDirective>
        </MapsComponent>
);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);

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.

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';

export function App() {
   return(<MapsComponent >
            <Inject services={[MapsTooltip]} />
                <LayersDirective>
                    <LayerDirective shapeData={world_map}
                     tooltipSettings={ {
                        visible: true,
                        valuePath: 'name'
                    } }>
                    </LayerDirective>
                </LayersDirective>
            </MapsComponent>
    );
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
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';

export function App() {
   return(<MapsComponent >
            <Inject services={[MapsTooltip]} />
                <LayersDirective>
                    <LayerDirective shapeData={world_map}
                     tooltipSettings={ {
                        visible: true,
                        valuePath: 'name'
                    } }>
                    </LayerDirective>
                </LayersDirective>
            </MapsComponent>
    );
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);