Bing maps in EJ2 TypeScript Maps control

13 Jun 202411 minutes to read

Bing Maps is a online Maps provider, owned by Microsoft. As like OSM, it provide Maps tile images based on our requests and combines those images into a single one to display Maps area.

Adding Bing Maps

The Bing Maps can be rendered using the urlTemplate property, which is based on the URL generated by the getBingUrlTemplate method in the Maps. The format of the required URL of Bing Maps varies from other online map providers. As a result, a built-in getBingUrlTemplate method has been included that returns the URL in a generic format. In the meantime, a subscription key is required for Bing Maps. The Bing Maps key can be obtained from here, then append it to the Bing Maps URL before passing it to the getBingUrlTemplate method. The URL returned by this method must be passed to the urlTemplate property.

import { Maps } from '@syncfusion/ej2-maps';
let map: Maps = new Maps({
    load: function(args){
        args.maps.getBingUrlTemplate("https://dev.virtualearth.net/REST/V1/Imagery/Metadata/Aerial?output=json&uriScheme=https&key=?").then(function(url) {
            args.maps.layers[0].urlTemplate= url;
        });
    },
    layers:[{

    }]
});
map.appendTo('container');

Types of Bing Maps

Bing Maps provides different types of Maps and it is supported in the Maps control.

  • Aerial - Displays satellite images to highlight roads and major landmarks for easy identification.
  • AerialWithLabel - Displays aerial Maps with labels for the continent, country, ocean, etc.
  • Road - Displays the default Maps view of roads, buildings, and geography.
  • CanvasDark - Displays dark version of the road Maps.
  • CanvasLight - Displays light version of the road Maps.
  • CanvasGray - Displays grayscale version of the road Maps.

To render the light version of the road Maps, set the CanvasLight value is passed via the URL into the getBingUrlTemplate method demonstrated in the following code sample.

import { Maps } from '@syncfusion/ej2-maps';
let map: Maps = new Maps({
    load: function(args){
        args.maps.getBingUrlTemplate("https://dev.virtualearth.net/REST/V1/Imagery/Metadata/CanvasLight?output=json&uriScheme=https&key=?").then(function(url) {
            args.maps.layers[0].urlTemplate= url;
        });
    },
    zoomSettings: {
        zoomFactor: 12,
    },
    centerPosition : {
        latitude: 38.8951,
        longitude: -77.0364
    },
    layers:[{
    }]
});
map.appendTo('container');

Enabling zooming and panning

Bing Maps layer can be zoomed and panned. Zooming helps to get a closer look at a particular area on a Maps for in-depth analysis. Panning helps to move a Maps around to focus the targeted area.

import { Maps } from '@syncfusion/ej2-maps';
let map: Maps = new Maps({
    load: function(args){
        args.maps.getBingUrlTemplate("https://dev.virtualearth.net/REST/V1/Imagery/Metadata/Aerial?output=json&uriScheme=https&key=?").then(function(url) {
            args.maps.layers[0].urlTemplate= url;
        });
    },
    zoomSettings: {
        enable: true,
        toolbarSettings: {
            buttonSettings: {
                toolbarItems: ['Zoom', 'ZoomIn', 'ZoomOut', 'Pan', 'Reset']
            }
        }
    },
    layers:[{
    }]
});
map.appendTo('container');

Adding markers and navigation line

Markers can be added to the layers of Bing Maps by setting the corresponding location’s coordinates of latitude and longitude using markerSettings. Navigation lines can be added on top of an Bing Maps layer for highlighting a path among various places by setting the corresponding location’s coordinates of latitude and longitude in the navigationLineSettings.

import { Maps } from '@syncfusion/ej2-maps';
Maps.Inject(NavigationLine, Marker, Zoom);
let map: Maps = new Maps({
    load: function(args){
        args.maps.getBingUrlTemplate("https://dev.virtualearth.net/REST/V1/Imagery/Metadata/Aerial?output=json&uriScheme=https&key=?").then(function(url) {
            args.maps.layers[0].urlTemplate= url;
        });
    },
    zoomSettings: {
        zoomFactor: 4
    },
    centerPosition: {
        latitude: 29.394708
        longitude: -94.954653
    },
    layers: [{
        markerSettings: [{
            visible: true,
            height: 25,
            width: 15,
            dataSouce: [{
                latitude: 34.060620,
                longitude: -118.330491,
                name: "California"
            },
            {
                latitude: 40.724546,
                longitude: -73.850344,
                name: "New York"
            }],
        }],
        navigationLineSettings: [{
            visible: true,
            color: "blue",
            angle: 0.1,
            latitude: {
                34.060620, 40.724546
            },
            longitude: {
                -118.330491, -73.850344
            }
        }]
    }]
});
map.appendTo('container');

Adding sublayer

Any GeoJSON shape can be rendered as a sublayer on top of the Bing Maps layer for highlighting a particular continent or country in Bing Maps by adding another layer and specifying the type property of Maps layer to SubLayer.

import { Maps } from '@syncfusion/ej2-maps';
import { Africa_Continent } from './Africa_Continent.ts';

let map: Maps = new Maps({
    load: function(args){
        args.maps.getBingUrlTemplate("https://dev.virtualearth.net/REST/V1/Imagery/Metadata/Aerial?output=json&uriScheme=https&key=?").then(function(url) {
            args.maps.layers[0].urlTemplate= url;
        });
    },
    layers: [
        {
            // Add bing map.
        },
        {
            type: 'SubLayer',
            shapeData: Africa_Continent,
            shapeSettings: {
                fill: 'blue'
            }
        }
    ]
});

map.appendTo("#container");

Enabling legend

The legend can be added to the tile Maps by setting the visible property of legendSettings to true.

import { Maps } from '@syncfusion/ej2-maps';
import { markerDataSource } from './markerdata.ts';
Maps.Inject(Legend, Marker);
let map: Maps = new Maps({
    load: function(args){
        args.maps.getBingUrlTemplate("https://dev.virtualearth.net/REST/V1/Imagery/Metadata/Aerial?output=json&uriScheme=https&key=?").then(function(url) {
            args.maps.layers[0].urlTemplate= url;
        });
    },
    legendSettings: {
        visible: true,
        type: 'Markers',
        useMarkerShape:true,
        toggleLegendSettings: {
          enable: true,
          applyShapeSettings: false,
          border: {
            color: 'green',
            width: 2
          }
        }
    },
    layers: [
        {
          shapeDataPath: 'name',
          shapePropertyPath: 'name',
          shapeSettings: {
            fill: '#E5E5E5'
          },
          markerSettings: [
            {
              dataSource: markerDataSource,
              colorValuePath: 'color',
              shapeValuePath:'shape',
              legendText: 'country',
              visible: true
            }
          ]
        }
    ]
});

map.appendTo("#container");