Overview of JavaScript Maps Control in Essential JS 2 | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (174).NET Core  (29).NET MAUI  (207)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (215)BoldSign  (14)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (66)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (100)Streamlit  (1)Succinctly series  (131)Syncfusion  (915)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (36)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (147)Chart  (131)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (628)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (40)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (507)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (592)What's new  (332)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)

Overview of JavaScript Maps Control in Essential JS 2

The maps control for Essential JS 2 is used to visualize geographical data and represent statistical data of a particular geographic region on Earth. It includes functionalities such as data binding, legends, markers, data labels, tooltips, zooming, and panning. Some of its advanced features include projections, bubbles, animation, color mapping, annotations, navigation lines, and more.

This blog post explores the Syncfusion maps control in Essential JS 2, which allows you to render shapes from provided GeoJSON data. We are going to render a real-time world map and represent the permanent and non-permanent countries in the United Nations Security Council for the year 2017 with various colors, titles, subtitles, legends, and tooltips. The following screenshot depicts the map we will build in this blog.

Map information sourced from Wikipedia

Installation and configuration

The maps control can be configured in TypeScript as follows:

    1. Clone the Essential JS 2 quick-start seed repository, and configure the drop-down package in the system.config.js file.
"@syncfusion/ej2-base": "syncfusion:ej2-base/dist/ej2-base.umd.min.js",
"@syncfusion/ej2-data": "syncfusion:ej2-data/dist/ej2-data.umd.min.js",
"@syncfusion/ej2-buttons": "syncfusion:ej2-buttons/dist/ej2-buttons.umd.min.js",
"@syncfusion/ej2-popups": "syncfusion:ej2-popups/dist/ej2-popups.umd.min.js",
"@syncfusion/ej2-maps": "syncfusion:ej2-maps/dist/ej2-maps.umd.min.js",
"@syncfusion/ej2-pdf-export": "syncfusion:ej2-pdf-export/dist/ej2-pdf-export.umd.min.js",
"@syncfusion/ej2-compression": "syncfusion:ej2-compression/dist/ej2-compression.umd.min.js",
"@syncfusion/ej2-file-utils": "syncfusion:ej2-file-utils/dist/ej2-file-utils.umd.min.js"
    1. Install the NPM packages by using the following command.
$ npm install
    1. Add the HTML input element that needs to be initialized as a component in the index.html file.

 

  • Initialize the component in the app.ts file as shown in the following code.
    import { Maps } from '@syncfusion/ej2-maps';
    // initialize maps component
    let map: Maps = new Maps();
    // render initialized maps
    map.appendTo('#container');
    

 

Configuring maps with GeoJSON data

After creating the maps component, bind the shape data to render the shapes. Any GeoJSON data can be specified as shape data.

export let World_Map: Object =
    {
        "type": "FeatureCollection",
        "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
        "features": [
           { "type": "Feature", "properties": { "admin": "Afghanistan", "name": "Afghanistan", "continent": "Asia" }, "geometry": { "type": "Polygon", "coordinates": [[[61.21081709172573, 35.650072333309218], [62.230651483005879, 35.270663967422287], 
            //..
            ]]}},
            //..
           ]
    };

A map built with the maps control is composed of layers, and can accommodate one or more layers. Shape data is bound to maps using the shapeData property in layers.

import { Maps } from '@syncfusion/ej2-maps';
let maps: Maps = new Maps({
    layers: [
        {
            shapeData: World_Map
        }
    ]
});

Bind data source

Define an array of JavaScript objects as a data source to the maps control. This data source will be further used to color the map, display data labels, display tooltips, and more. Assign this to the dataSource property in layers.

export let unCountries: object[] = [
    { Country: 'China', Membership: 'Permanent' },
    { Country: 'France', Membership: 'Permanent' },
    { Country: 'Russia', Membership: 'Permanent' },
    { Country: 'United Kingdom', Membership: 'Permanent' },
    { Country: 'United States', Membership: 'Permanent' },
    { Country: 'Bolivia', Membership: 'Non-Permanent' },
    { Country: 'Eq. Guinea', Membership: 'Non-Permanent' },
    { Country: 'Ethiopia', Membership: 'Non-Permanent' },
    { Country: 'Ivory Coast', Membership: 'Permanent' },
    { Country: 'Kazakhstan', Membership: 'Non-Permanent' },
    { Country: 'Kuwait', Membership: 'Non-Permanent' },
    { Country: 'Netherlands', Membership: 'Non-Permanent' },
    { Country: 'Peru', Membership: 'Non-Permanent' },
    { Country: 'Poland', Membership: 'Non-Permanent' },
    { Country: 'Sweden', Membership: 'Non-Permanent' }
];

You should also specify the field names in the shape data and data source to the shapePropertyPath and shapeDataPath properties, respectively. These are used to identify the appropriate shapes and match the specific data source values to them.

For example, consider the color value specified in the data source and the field names specified in shapePropertyPath and shapeDataPath have the same value: ‘Australia’. That color will be applied to the Australia shape.

import { Maps } from '@syncfusion/ej2-maps';
let maps: Maps = new Maps({
    layers: [
        {
            shapeData: World_Map,
            dataSource: unCountries,
            shapePropertyPath: 'name',
            shapeDataPath: 'Country'
        }
    ]
});

Mapping colors

The desired fill colors can be directly specified for shapes. You can also apply colors based on the desired conditions. Here, the field that is used for comparison is specified in the colorValuePath property. If the value of a shape is ‘Permanent’, then the color ‘#EDB46F’ will be applied to the shape. If the value of the shape is ‘Non-Permanent’, the color ‘#F1931B’ will be applied.

import { Maps } from '@syncfusion/ej2-maps';
let maps: Maps = new Maps({
    layers: [
        {
            shapeData: World_Map,
            dataSource: unCountries,
            shapeDataPath: 'Country',
            shapePropertyPath: 'name',
            shapeSettings: {
                fill: '#E5E5E5',
                colorValuePath: 'Membership',
                colorMapping: [
                    {
                        value: 'Permanent',
                        color: '#EDB46F'
                    },
                    {
                        value: 'Non-Permanent',
                        color: '#F1931B'
                    }
                ]
            }
        }
    ]
});

Define title and subtitle

Specify the appropriate title and subtitle to portray additional information about the map. This can be done by specifying string values to the text property in titleSettings and subtitleSettings.

import { Maps } from '@syncfusion/ej2-maps';
let maps: Maps = new Maps({
    titleSettings: {
        text: 'Members of the UN Security Council',
        subtitleSettings: {
            text: '- In 2017',
            textAlignment: 'Far'
        }
    }
});

Display legend

Legend items are rendered based on the color mapping values. In our example, this means the legend will be displayed based on the ‘Permanent’ and ‘Non-Permanent’ values specified in the color mapping. You can display the map legend by setting the visible property to true in legendSettings.

import { Maps, Legend } from '@syncfusion/ej2-maps';
Maps.Inject(Legend);
let maps: Maps = new Maps({
    legendSettings: {
        visible: true
    }
});

Enable tooltips

Tooltips can be enabled to display additional information about shapes when the pointer hovers over them. The values displayed in a tooltip can be retrieved from the data source by specifying the data source’s field name in the valuePath property and setting the visible property to true in tooltipSettings.

import { Maps, MapsTooltip} from '@syncfusion/ej2-maps';
Maps.Inject(MapsTooltip);
let maps: Maps = new Maps({
    layers: [
        {
            tooltipSettings: {
                visible: true,
                valuePath: 'Country'
            }
        }
    ]
});

The complete sample code is as follows.

import { Maps, Legend, MapsTooltip } from '@syncfusion/ej2-maps';
import {World_Map} from './WorldMap';
Maps.Inject(Legend, MapsTooltip);

export let unCountries: object[] = [
    { Country: 'China', Membership: 'Permanent' },
    { Country: 'France', Membership: 'Permanent' },
    { Country: 'Russia', Membership: 'Permanent' },
    { Country: 'United Kingdom', Membership: 'Permanent' },
    { Country: 'United States', Membership: 'Permanent' },
    { Country: 'Bolivia', Membership: 'Non-Permanent' },
    { Country: 'Eq. Guinea', Membership: 'Non-Permanent' },
    { Country: 'Ethiopia', Membership: 'Non-Permanent' },
    { Country: 'Ivory Coast', Membership: 'Permanent' },
    { Country: 'Kazakhstan', Membership: 'Non-Permanent' },
    { Country: 'Kuwait', Membership: 'Non-Permanent' },
    { Country: 'Netherlands', Membership: 'Non-Permanent' },
    { Country: 'Peru', Membership: 'Non-Permanent' },
    { Country: 'Poland', Membership: 'Non-Permanent' },
    { Country: 'Sweden', Membership: 'Non-Permanent' }
];

// initialize Maps component
let map: Maps = new Maps({
    titleSettings: {
        text: 'Members of the UN Security Council',
        subtitleSettings: {
            text: '- In 2017',
            alignment: 'Far'
        }
    },
    legendSettings: {
        visible: true
    },
    layers: [
        {
            shapeData: World_Map,
            dataSource : unCountries,
            shapePropertyPath: 'name',
            shapeDataPath: 'Country',
            tooltipSettings: {
                visible: true,
                valuePath: 'Country'
            },
            shapeSettings: {
                fill: '#E5E5E5',
                colorValuePath: 'Membership',
                colorMapping: [
                    {
                        value: 'Permanent',
                        color: '#EDB46F'
                    },
                    {
                        color: '#F1931B',
                        value: 'Non-Permanent'
                    }
                ]
            }
        }
    ]
});

// render initialized Map
map.appendTo('#container');

An interactive version of this sample is available on StackBlitz: https://stackblitz.com/edit/wvprqu?file=index.ts

Conclusion

In this blog, we hope that you have learned how to create a simple map and enhance its basic features. The maps control has many advanced features, such as rendering for other map providers, custom shapes, projections, markers, data labels, bubbles, animations, navigation lines, and user-interactive features such as zooming and panning, selection, highlighting, and drill-down. In future blogs we will see how easy it is to configure some of these advanced features.

If you have any questions or need any clarifications, please let us know in the comments section below. You can also contact us through our support forum or Direct-Trac. We are always happy to assist you!

Helpful links

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed