Bing maps in Angular Maps component
13 Jun 202413 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 { Component, OnInit } from '@angular/core';
import { ILoadEventArgs } from '@syncfusion/ej2-angular-maps';
@Component({
selector: 'app-container',
template:
`<ejs-maps id='rn-container' (load)="load($event)" style="display:block">
<e-layers>
<e-layer></e-layer>
</e-layers>
</ejs-maps>`
})
export class AppComponent implements OnInit {
public load = (args: ILoadEventArgs) : void => {
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;
});
};
}
Types of Bing Maps
Bing Maps provides different types of Maps and it is supported in the Maps component.
- 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 { Component, OnInit} from '@angular/core';
import { ILoadEventArgs } from '@syncfusion/ej2-angular-maps';
@Component({
selector: 'app-container',
template:
`<ejs-maps id='rn-container' (load)="load($event)" style="display:block">
<e-layers>
<e-layer></e-layer>
</e-layers>
</ejs-maps>`
})
export class AppComponent implements OnInit {
public load = (args: ILoadEventArgs) : void => {
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;
});
};
}
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 { Component, OnInit, Inject } from '@angular/core';
import { Maps, Zoom, ILoadEventArgs } from '@syncfusion/ej2-angular-maps';
Maps.Inject(Zoom);
@Component({
selector: 'app-container',
template: `
<ejs-maps id="rn-container" (load)="load($event)" [zoomSettings]="zoomSettings" style="display:block">
<e-layers>
<e-layer></e-layer>
</e-layers>
</ejs-maps>
`
})
export class AppComponent implements OnInit {
public zoomSettings: object;
ngOnInit(): void {
this.zoomSettings = {
enable:true,
toolbarSettings: {
buttonSettings: {
toolbarItems: ['Zoom', 'ZoomIn', 'ZoomOut', 'Pan', 'Reset'],
}
}
};
public load = (args: ILoadEventArgs) : void => {
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;
});
};
}
}
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 { Component, OnInit, Inject } from '@angular/core';
import { Maps, Zoom, Marker, NavigationLine, ILoadEventArgs } from '@syncfusion/ej2-angular-maps';
Maps.Inject(Zoom, Marker, NavigationLine);
@Component({
selector: 'app-container',
template: `
<ejs-maps id="rn-container" (load)="load($event)" style="display:block" [zoomSettings]="zoomSettings" [centerPosition]="centerPosition">
<e-layers>
<e-layer [markerSettings]="markerSettings" [navigationLineSettings]="navigationLineSettings"></e-layer>
</e-layers>
</ejs-maps>
`
})
export class AppComponent implements OnInit {
public zoomSettings: object;
public centerPosition: object;
public markerSettings: object;
public navigationLineSettings: object;
public load = (args: ILoadEventArgs) : void => {
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;
});
};
ngOnInit(): void {
this.zoomSettings = {
zoomFactor: 4
};
this.centerPosition = {
latitude: 29.394708,
longitude: -94.954653
};
this.markerSettings = [
{
visible: true,
height: 25,
width: 15,
dataSource: [
{
latitude: 34.06062,
longitude: -118.330491,
name: 'California'
},
{
latitude: 40.724546,
longitude: -73.850344,
name: 'New York'
}
]
}
];
this.navigationLineSettings = [
{
visible: true,
color: 'blue',
width: 5,
angle: 0.1,
latitude: [34.06062, 40.724546],
longitude: [-118.330491, -73.850344]
}];
}
}
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 { Component, OnInit } from '@angular/core';
import { Maps, ILoadEventArgs } from '@syncfusion/ej2-angular-maps';
import { africa_continent } from 'africa-continent.ts';
@Component({
selector: 'app-container',
template:
`<ejs-maps (load)="load($event)" id='rn-container' style="display:block">
<e-layers>
<e-layer></e-layer>
<e-layer [shapeData]= 'shapeData' [type] ='Sublayer' [shapeSettings]='shapeSettings'></e-layer>
</e-layers>
</ejs-maps>`
})
export class AppComponent implements OnInit {
public load = (args: ILoadEventArgs) : void => {
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;
});
};
public shapeData: object;
public shapeSettings: object;
ngOnInit(): void {
this.shapeData = africa_continent;
this.shapeSettings = {
fill: 'blue'
}
}
}
Enabling legend
The legend can be added to the tile Maps by setting the visible property of legendSettings to true.
import { Component, OnInit } from '@angular/core';
import { Maps, ILoadEventArgs } from '@syncfusion/ej2-angular-maps';
import { markerDataSource } from 'markerdata.ts';
@Component({
selector: 'app-container',
template:
`<ejs-maps
id="rn-container"
(load)="load($event)"
style="display:block"
[legendSettings]="legendSettings"
>
<e-layers>
<e-layer
[shapePropertyPath]="shapePropertyPath"
[shapeDataPath]="shapeDataPath"
[markerSettings]="markerSettings"
[shapeSettings]="shapeSettings"
></e-layer>
</e-layers>
</ejs-maps>`
})
export class AppComponent implements OnInit {
public load = (args: ILoadEventArgs): void => {
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;
});
};
public shapeDataPath: string;
public shapePropertyPath: string;
public shapeSettings: object;
public markerSettings: object;
public legendSettings: object;
ngOnInit(): void {
this.shapeDataPath = 'name';
this.shapePropertyPath = 'name';
this.legendSettings = {
visible: true,
type: 'Markers',
useMarkerShape: true,
toggleLegendSettings: {
enable: true,
applyShapeSettings: false,
border: {
color: 'green',
width: 2,
},
},
};
this.shapeSettings = {
fill: '#E5E5E5',
};
this.markerSettings = [
{
dataSource: markerDataSource,
colorValuePath: 'color',
shapeValuePath: 'shape',
legendText: 'country',
visible: true,
},
];
}
}