Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
<!DOCTYPE html>
<html lang="en">
<head>
<title>Create a Choropleth Map - 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 choropleth map. A choropleth map is a thematic map in which areas are shaded or patterned in proportion to the measurement of the statistical variable being displayed on the map, in this case the population density of each state." />
<meta name="keywords" content="Microsoft maps, map, gis, API, SDK, thematic, choropleth, heatmap, heat map" />
<meta name="author" content="Microsoft Azure Maps" />
<!-- Add references to the Azure Maps Map control JavaScript and CSS files. -->
<link rel="stylesheet" href="https://atlas.microsoft.com/sdk/javascript/mapcontrol/2/atlas.min.css" type="text/css" />
<script src="https://atlas.microsoft.com/sdk/javascript/mapcontrol/2/atlas.min.js"></script>
<script type='text/javascript'>
var map, datasource, popup, maxValue = 500;
var defaultColor = '#FFEDA0';
var colorScale = [
10, '#FED976',
20, '#FEB24C',
50, '#FD8D3C',
100, '#FC4E2A',
200, '#E31A1C',
500, '#BD0026',
1000, '#800026'
];
function GetMap() {
//Point the Azure Maps domain to the US Azure Gov Cloud domain.
atlas.setDomain('atlas.azure.us');
//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 = 'https://azuremapscodesamples.azurewebsites.us/Common/TokenService.ashx';
fetch(tokenServiceUrl).then(r => r.text()).then(token => resolve(token));
}
//Alternatively, use an Azure Maps key. Get an Azure Maps key at https://azure.com/maps. NOTE: The primary key should be used as the key.
//authType: 'subscriptionKey',
//subscriptionKey: '<Your Azure Maps Key>'
}
});
//Create a legend.
createLegend();
//Wait until the map resources are ready.
map.events.add('ready', function () {
//Create a popup but leave it closed so we can update it and display it later.
popup = new atlas.Popup({
position: [0, 0]
});
//Create a data source and add it to the map.
datasource = new atlas.source.DataSource();
map.sources.add(datasource);
//Create a stepped expression based on the color scale.
var steppedExp = [
'step',
['get', 'density'],
defaultColor
];
steppedExp = steppedExp.concat(colorScale);
//Create a layer to render the polygon data.
var polygonLayer = new atlas.layer.PolygonLayer(datasource, null, {
fillColor: steppedExp
});
map.layers.add(polygonLayer, 'labels');
//Add a mouse move event to the polygon layer to show a popup with information.
map.events.add('mousemove', polygonLayer, function (e) {
if (e.shapes && e.shapes.length > 0) {
var properties = e.shapes[0].getProperties();
//Update the content of the popup.
popup.setOptions({
content: '<div style="padding:10px"><b>' + properties.name + '</b><br/>Population Density: ' + properties.density + ' people/mi<sup>2</sup></div>',
position: e.position
});
//Open the popup.
popup.open(map);
}
});
//Add a mouse leave event to the polygon layer to hide the popup.
map.events.add('mouseleave', polygonLayer, function (e) {
popup.close();
});
//Download a GeoJSON feed and add the data to the data source.
datasource.importDataFromUrl('/Common/data/geojson/US_States_Population_Density.json');
});
}
function createLegend() {
var html = [];
html.push('<i style="background:', defaultColor, '"></i> 0-', colorScale[0], '<br/>');
for (var i = 0; i < colorScale.length; i += 2) {
html.push(
'<i style="background:', (colorScale[i + 1]), '"></i> ',
colorScale[i], (colorScale[i + 2] ? '&ndash;' + colorScale[i + 2] + '<br/>' : '+')
);
}
document.getElementById('legend').innerHTML += html.join('');
}
</script>
<style>
#legend {
position: absolute;
top: 5px;
left: 5px;
font-family: Arial;
font-size: 12px;
background-color: rgba(255, 255, 255, 0.8);
border-radius: 5px;
padding: 5px;
line-height:18px;
}
#legend i {
width: 18px;
height: 18px;
float: left;
margin-right: 8px;
opacity: 0.7;
}
</style>
</head>
<body onload="GetMap()">
<div id="myMap" style="position:relative;width:100%;min-width:290px;height:600px;"></div>
<div id="legend">Population Density<br/>(people/mi<sup>2</sup>)<br/></div>
<fieldset style="width:calc(100% - 30px);min-width:290px;margin-top:10px;">
<legend><h1 style="font-size:16px">Create a Choropleth Map</h1></legend>
This sample shows how to create a choropleth map. A choropleth map is a thematic map in which areas are shaded or patterned in
proportion to the measurement of the statistical variable being displayed on the map, in this case the population density of each state.
</fieldset>
</body>
</html>