HelpBot Assistant

How can I help you?

Populate data in React Maps component

20 Feb 202624 minutes to read

Geometry types

GeoJSON data contains geometry objects with properties such as geometry types and coordinates. The geometry types are the values present in the geometry objects of the GeoJSON data that specify the type of shape to be rendered, as well as the coordinates that help to draw the shape’s boundary line. The supportive geometry types are:

Shapes Supported
Polygon Yes
MultiPolygon Yes
LineString Yes
MultiLineString Yes
Point Yes
MultiPoint Yes
GeometryCollection Yes

Shape data

The shape data collection describes geographical shape information that is available in GeoJSON format. The map shapes are rendered using this data. Custom shapes such as seat selection in a bus, seat selection in a cricket stadium, and other useful information can also be added as shapeData in the layer of the Maps.

export let usMap = // Paste all the content copied from the JSON file.

Data source

The dataSource property is used to represent statistical data in the Maps component, and it accepts a collection of values as input. For example, a list of objects can be provided as input to the data source. This data source is used to color the map, display data labels, and display tooltips, among other things.

The data source is populated with JSON data relative to shape data and stored as a JSON object. In the following example, populationData can be used as a data source in Maps.

export let populationData: object[] = [
    {
        'code': 'AF',
        'value': 53,
        'name': 'Afghanistan',
        'population': 29863010,
        'density': 119
    },
    {
        'code': 'AL',
        'value': 117,
        'name': 'Albania',
        'population': 3195000,
        'density': 111
    },
    {
        'code': 'DZ',
        'value': 15,
        'name': 'Algeria',
        'population': 34895000,
        'density': 15
    },
    {
        'code': 'AO',
        'value': 15,
        'name': 'Angola',
        'population': 18498000,
        'density': 15
    },
    {
        'code': 'AR',
        'value': 15,
        'name': 'Argentina',
        'population': 40091359,
        'density': 14
    },
    {
        'code': 'AM',
        'value': 109,
        'name': 'Armenia',
        'population': 3230100,
        'density': 108
    }
];

Data binding

The following properties in the layers are used for binding data in the Maps component. These two properties work together to establish the relationship between shape data and the data source.

  • shapePropertyPath
  • shapeDataPath

shapePropertyPath

The shapePropertyPath property is used to refer the field name in the shapeData property of shape layers to identify the shape. When the values of shapeDataPath property from the dataSource property and shapePropertyPath property from the shapeData property match, then the associated object from the data source is bound to the corresponding shape.

world-map.ts file contains following data and its field name value is used to map the corresponding shape with the provided data source.

export let world_map: object = {
    "type": "Feature",
    "properties": {
        "admin": "Afghanistan",
        "name": "Afghanistan",
        "continent": "Asia"
    },
    "geometry": { "type": "Polygon", "coordinates": [[[61.21081709172573, https://ej2.syncfusion.com/react/documentation. },
...

shapeDataPath

The shapeDataPath property is similar to the shapePropertyPath property, but it refers to the field name in the dataSource property. For example, populationData contains the code, value, name, population, and density fields. Here, the name field is set to the shapeDataPath to map the corresponding value of the field name in shape data.

In the following example, both name fields contain the same value Afghanistan. This value is matched in both the shape data and data source, so the details associated with Afghanistan are mapped to the corresponding shape and used to color the shape, display data labels, display tooltips, and more.

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 >
                <LayersDirective>
                    <LayerDirective shapeData={world_map} shapeDataPath='name' shapePropertyPath='name'
                                    dataSource={[
                                        {
                                            'code': 'AF',
                                            'value': 53,
                                            'name': 'Afghanistan',
                                            'population': 29863010,
                                            'density': 119,
                                            'color': '#DEEBAE'
                                        },
                                        {
                                            'code': 'AL',
                                            'value': 117,
                                            'name': 'Albania',
                                            'population': 3195000,
                                            'density': 111,
                                            'color': '#A4D6AD'
                                        },
                                        {
                                            'code': 'DZ',
                                            'value': 15,
                                            'name': 'Algeria',
                                            'population': 34895000,
                                            'density': 15,
                                            'color': '#37AFAB'
                                        },
                                        {
                                            'code': 'AO',
                                            'value': 15,
                                            'name': 'Angola',
                                            'population': 18498000,
                                            'density': 15,
                                            'color': '#547C84'
                                        },
                                        {
                                            'code': 'AR',
                                            'value': 15,
                                            'name': 'Argentina',
                                            'population': 40091359,
                                            'density': 14,
                                            'color': '#CEBF93'
                                        },
                                        {
                                            'code': 'AM',
                                            'value': 109,
                                            'name': 'Armenia',
                                            'population': 3230100,
                                            'density': 108,
                                            'color': '#a69d70'
                                        }
                                    ]}
                                    shapeSettings=>
                    </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 >
                <LayersDirective>
                    <LayerDirective shapeData={world_map} shapeDataPath='name' shapePropertyPath='name'
                                    dataSource={[
                                        {
                                            'code': 'AF',
                                            'value': 53,
                                            'name': 'Afghanistan',
                                            'population': 29863010,
                                            'density': 119,
                                            'color': '#DEEBAE'
                                        },
                                        {
                                            'code': 'AL',
                                            'value': 117,
                                            'name': 'Albania',
                                            'population': 3195000,
                                            'density': 111,
                                            'color': '#A4D6AD'
                                        },
                                        {
                                            'code': 'DZ',
                                            'value': 15,
                                            'name': 'Algeria',
                                            'population': 34895000,
                                            'density': 15,
                                            'color': '#37AFAB'
                                        },
                                        {
                                            'code': 'AO',
                                            'value': 15,
                                            'name': 'Angola',
                                            'population': 18498000,
                                            'density': 15,
                                            'color': '#547C84'
                                        },
                                        {
                                            'code': 'AR',
                                            'value': 15,
                                            'name': 'Argentina',
                                            'population': 40091359,
                                            'density': 14,
                                            'color': '#CEBF93'
                                        },
                                        {
                                            'code': 'AM',
                                            'value': 109,
                                            'name': 'Armenia',
                                            'population': 3230100,
                                            'density': 108,
                                            'color': '#a69d70'
                                        }
                                    ]}
                                    shapeSettings=>
                    </LayerDirective>
                </LayersDirective>
            </MapsComponent>
    );
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);

Binding complex data source

Data from a data source can be bound to the Maps in two different ways.

  1. Bind the field name directly to properties such as shapeDataPath, colorValuePath,
    valuePath, and shapeValuePath.

  2. Bind the field name as data.field to properties such as shapeDataPath, colorValuePath,
    valuePath, and shapeValuePath.

import { world_map } from 'world-map.ts';
import { complexData } from 'data.ts'
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective, Inject, MapsTooltip } from '@syncfusion/ej2-react-maps';
import { BubblesDirective, BubbleDirective, Bubble } from '@syncfusion/ej2-react-maps';
import { MarkersDirective, MarkerDirective, Marker } from '@syncfusion/ej2-react-maps';
export function App() {
    return(
            <MapsComponent >
			<Inject services={[Marker, Bubble, MapsTooltip]}/>
                <LayersDirective>
                    <LayerDirective shapeData={world_map} shapeDataPath='data.continent' shapePropertyPath='continent' dataSource={complexData}
					shapeSettings={{
					colorValuePath:'data.color'
					}} tooltipSettings={{
						visible: true,
						valuePath: 'data.continent'
					}}>
					<BubblesDirective>
                            <BubbleDirective visible={true} valuePath="data.value"
                            animationDuration={0} opacity={0.8} minRadius={20} maxRadius={90}
							dataSource={[
                                { 'name': 'India', 'value': 18.89685398845257, 'population': 391292635,
                                data: { 'color': 'red', 'population': 391292635,
                                'value': 189685398845257 }
                                }]}
					            tooltipSettings={{
				            		visible: true,
                                    valuePath: 'data.population',
                                    template:"<div>${data.population}</div>"
					            }}/>
                    </BubblesDirective>
					<MarkersDirective>
                        <MarkerDirective visible={true} height={20} animationDuration={0}
                            width={20}  longitudeValuePath= "data.y" latitudeValuePath= "data.x" shapeValuePath= "data.shape" colorValuePath= "data.color"
				            tooltipSettings={{
					            visible: true,
                                valuePath: 'data.name',
                                format: "${data.name}: ${data.x} : ${data.y}"
				            }}
				            offset = {{
				            	y: -10,
                                x: 0
				            }}
                            dataSource={[
                                { latitude: 37.6276571, longitude: -122.4276688,
                                name: 'San Bruno',
                                data: { x: 37.6276571, y: -122.4276688, name: 'San Bruno',
                                shape: 'Pentagon', color: 'red', imageUrl: 'images/ballon.png' }
                                },
                                { latitude: 33.5302186, longitude: -117.7418381, name: 'Laguna Niguel',
                                data: { x: 33.5302186, y: -117.7418381, name: 'Laguna Niguel',
                                color: 'blue', shape: 'Pentagon', imageUrl: 'images/ballon.png' }
                            }]}>
                        </MarkerDirective>
					</MarkersDirective>
                    </LayerDirective>
                </LayersDirective>
            </MapsComponent>
    );
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
import { world_map } from 'world-map.ts';
import { complexData } from 'data.ts'
import * as React from "react";
import * as ReactDOM from "react-dom";
import { MapsComponent, LayersDirective, LayerDirective, Inject, MapsTooltip } from '@syncfusion/ej2-react-maps';
import { BubblesDirective, BubbleDirective, Bubble } from '@syncfusion/ej2-react-maps';
import { MarkersDirective, MarkerDirective, Marker } from '@syncfusion/ej2-react-maps';
export function App() {
    return(
            <MapsComponent >
			<Inject services={[Marker, Bubble, MapsTooltip]}/>
                <LayersDirective>
                    <LayerDirective shapeData={world_map} shapeDataPath='data.continent' shapePropertyPath='continent' dataSource={complexData}
					shapeSettings={{
					colorValuePath:'data.color'
					}} tooltipSettings={{
						visible: true,
						valuePath: 'data.continent'
					}}>
					<BubblesDirective>
                            <BubbleDirective visible={true} valuePath="data.value"
                            animationDuration={0} opacity={0.8} minRadius={20} maxRadius={90}
							dataSource={[
                                { 'name': 'India', 'value': 18.89685398845257, 'population': 391292635,
                                data: { 'color': 'red', 'population': 391292635,
                                'value': 189685398845257 }
                                }]}
					            tooltipSettings={{
				            		visible: true,
                                    valuePath: 'data.population',
                                    template:"<div>${data.population}</div>"
					            }}/>
                    </BubblesDirective>
					<MarkersDirective>
                        <MarkerDirective visible={true} height={20} animationDuration={0}
                            width={20}  longitudeValuePath= "data.y" latitudeValuePath= "data.x" shapeValuePath= "data.shape" colorValuePath= "data.color"
				            tooltipSettings={{
					            visible: true,
                                valuePath: 'data.name',
                                format: "${data.name}: ${data.x} : ${data.y}"
				            }}
				            offset = {{
				            	y: -10,
                                x: 0
				            }}
                            dataSource={[
                                { latitude: 37.6276571, longitude: -122.4276688,
                                name: 'San Bruno',
                                data: { x: 37.6276571, y: -122.4276688, name: 'San Bruno',
                                shape: 'Pentagon', color: 'red', imageUrl: 'images/ballon.png' }
                                },
                                { latitude: 33.5302186, longitude: -117.7418381, name: 'Laguna Niguel',
                                data: { x: 33.5302186, y: -117.7418381, name: 'Laguna Niguel',
                                color: 'blue', shape: 'Pentagon', imageUrl: 'images/ballon.png' }
                            }]}>
                        </MarkerDirective>
					</MarkersDirective>
                    </LayerDirective>
                </LayersDirective>
            </MapsComponent>
    );
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);