Skip to content
Switch branches/tags
Go to file
Cannot retrieve contributors at this time
<!DOCTYPE html>
<html lang="en">
<title>Polygon Hover Style - Azure Maps Web SDK Samples</title>
<meta charset="utf-8" />
<link rel="shortcut icon" href="/favicon.ico"/>
<meta http-equiv="x-ua-compatible" content="IE=Edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="This sample shows how to create a hover effect on a polygon layer." />
<meta name="keywords" content="Microsoft maps, map, gis, API, SDK, polygon, layer, hover, mouse" />
<meta name="author" content="Microsoft Azure Maps" />
<!-- Add references to the Azure Maps Map control JavaScript and CSS files. -->
<link rel="stylesheet" href="" type="text/css" />
<script src=""></script>
<script type='text/javascript'>
var map, datasource;
var stateBoundaries = '../Common/data/geojson/US_States_500k.json';
function GetMap() {
//Point the Azure Maps domain to the US Azure Gov Cloud domain.
//Initialize a map instance.
map = new atlas.Map('myMap', {
center: [-110, 50],
zoom: 2,
view: 'Auto',
//Add authentication details for connecting to Azure Maps.
authOptions: {
//Use Azure Active Directory authentication.
authType: 'anonymous',
clientId: 'c9f2f391-13f1-407b-a4a5-f0a241bacfbf', //Your Azure Active Directory client id for accessing your Azure Maps account.
getToken: function (resolve, reject, map) {
//URL to your authentication service that retrieves an Azure Active Directory Token.
var tokenServiceUrl = '';
fetch(tokenServiceUrl).then(r => r.text()).then(token => resolve(token));
//Alternatively, use an Azure Maps key. Get an Azure Maps key at NOTE: The primary key should be used as the key.
//authType: 'subscriptionKey',
//subscriptionKey: '<Your Azure Maps Key>'
//Wait until the map resources are ready.'ready', function () {
//Create a data source and add it to the map.
datasource = new atlas.source.DataSource();
//Load the US state boundary data.
//Add a layer for rendering filled polygon area.
var polygonLayer = new atlas.layer.PolygonLayer(datasource, null, {
fillColor: 'rgba(0, 157, 255, 0.2)'
//Add a layer for rendering a different color polygon for representing a hover state.
var polygonHoverLayer = new atlas.layer.PolygonLayer(datasource, null, {
fillColor: 'rgba(150, 50, 255, 0.2)',
//Only polygons with a "GEO_ID" property with a value of '' will be rendered.
filter: ['==', ['get', 'GEO_ID'], '']
//Add a layer for rendering the styled outline of the polygon data.
var lineLayer = new atlas.layer.LineLayer(datasource, null, {
strokeColor: 'white',
strokeWidth: 1
//Add the layers to be map, but render it below the labels.
map.layers.add([polygonLayer, polygonHoverLayer, lineLayer], 'labels');
// When the user moves their mouse over the polygonLayer, we'll update the filter in
// the polygonHoverLayer to only show the matching state, thus creating a hover effect.'mousemove', polygonLayer, function (e) {
polygonHoverLayer.setOptions({ filter: ['==', ['get', 'GEO_ID'], e.shapes[0].getProperties().GEO_ID] });
// Reset the polygonHoverLayer layer's filter when the mouse leaves the layer.'mouseleave', polygonLayer, function (e) {
polygonHoverLayer.setOptions({ filter: ['==', ['get', 'GEO_ID'], ''] });
<body onload="GetMap()">
<div id="myMap" style="position:relative;width:100%;min-width:290px;height:600px;"></div>
<fieldset style="width:calc(100% - 30px);min-width:290px;margin-top:10px;">
<legend><h1 style="font-size:16px">Polygon Hover Style</h1></legend>
This sample shows how to create a hover effect on a polygon layer.
The basic principal is to have two layers, one for rendering all data, and a seconded layer that only renders
the shape being hovered. A filter is used on the hover layer based on a unique property on each shape,
in this case a "GEO_ID" property.