Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
<!DOCTYPE html>
<html lang="en">
<head>
<title>Extruded gridded data source - 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 3D polygon extrusions from a gridded data source." />
<meta name="keywords" content="Microsoft maps, maps, map, API, SDK, GIS, bivariate, data binning, data bin, gridding, hexgrid, hexbins, hexagon bins, tessellations, thematic, choropleth, heatmap, heat map, polygon extrusion, extruded polygons" />
<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>
<!-- Add reference to the gridded data source module. -->
<script src="../Common/scripts/azure-maps-gridded-data-source.min.js"></script>
<script type='text/javascript'>
var map, datasource, popup;
var defaultColor = '#ffffb2';
var colorScale = [
100, '#fecc5c',
200, '#fd8d3c',
300, '#f03b20',
400, '#bd0026'
];
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: [-105.03, 40.54],
zoom: 11,
//Pitch the map so that the extrusion of the polygons is visible.
pitch: 45,
style: 'grayscale_dark',
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 reusable popup.
popup = new atlas.Popup();
//Create an instance of the gridded data source.
datasource = new atlas.source.GriddedDataSource(null, {
cellWidth: 0.25,
distanceUnits: 'miles',
//Reduce the coverage to give a bit of space between the grid basrs for better visual effect.
coverage: 0.8,
//Adjust the pixel scaling of the grid such that spatial distances are accurate near the center of the initial map view.
centerLatitude: map.getCamera().center[1]
});
map.sources.add(datasource);
//Load a dataset of polygons that have metadata we can style against.
datasource.importDataFromUrl('../Common/data/geojson/address.json').then(() => {
//Hide the loading icon.
document.getElementById('loadingIcon').style.display = 'none';
});
//Create a stepped expression based on the color scale.
var steppedExp = [
'step',
['get', 'point_count'],
defaultColor
];
steppedExp = steppedExp.concat(colorScale);
//Create an interpolate expression for height based on the `point_count` value of each cell.
var heightExp = [
'interpolate',
['linear'],
['get', 'point_count'],
1, 10, //When point count is 1, height is 10 meters.
1000, 5000 //When point_count is 1000, height is 1000 meters. All values between 1 and 5000 are interpolated.
];
//Create a polygon extrusion layer to render all cells of a grid with some opacity.
var polygonLayer = new atlas.layer.PolygonExtrusionLayer(datasource, null, {
fillColor: steppedExp,
fillOpacity: 0.8,
height: heightExp
});
//Create a second polygon extrusion layer to use a to highlight hovered grid cells by giving them a solid opacity.
var polygonHoverLayer = new atlas.layer.PolygonExtrusionLayer(datasource, null, {
fillColor: steppedExp,
fillOpacity: 1,
height: heightExp,
//Only polygons with a "cell_id" property with a value of '' will be rendered.
filter: ['==', ['get', 'cell_id'], '']
});
map.layers.add([polygonLayer, polygonHoverLayer], '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.
map.events.add('mousemove', polygonLayer, function (e) {
polygonHoverLayer.setOptions({ filter: ['==', ['get', 'cell_id'], e.shapes[0].getProperties().cell_id] });
map.getCanvasContainer().style.cursor = 'pointer';
});
// Reset the polygonHoverLayer layer's filter when the mouse leaves the layer.
map.events.add('mouseleave', polygonLayer, function (e) {
polygonHoverLayer.setOptions({ filter: ['==', ['get', 'cell_id'], ''] });
map.getCanvasContainer().style.cursor = 'grab';
});
//Add a click event to the polygon layer to display a popup.
map.events.add('click', polygonHoverLayer, openPopup);
});
}
function openPopup(e){
popup.setOptions({
position: e.position,
content: atlas.PopupTemplate.applyTemplate(e.shapes[0].getProperties())
});
popup.open(map);
}
function changeGridType(elm) {
datasource.setOptions({ gridType: elm.options[elm.selectedIndex].value });
}
function createLegend() {
var html = [];
html.push('<i style="background:', defaultColor, '"></i> 1 - ', 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] ? ' - ' + colorScale[i + 2] + '<br/>' : '+')
);
}
document.getElementById('legendColors').innerHTML += html.join('');
}
</script>
<style>
#legend {
position: absolute;
top: 1px;
left: 5px;
font-family: Arial;
font-size: 12px;
background-color: white;
border-radius: 5px;
padding: 5px;
line-height: 20px;
}
#legend i {
width: 18px;
height: 18px;
float: left;
margin-right: 8px;
}
</style>
</head>
<body onload="GetMap()">
<div id="myMap" style="position:relative;width:100%;min-width:290px;height:600px;"></div>
<img id="loadingIcon" src="../Common/images/loadingIcon.gif" title="Loading" style="position:absolute;left:calc(50% - 25px);top:250px;" />
<div id="legend">
<b>Addresses per cell</b><br />
<div id="legendColors"></div><br />
<b>Grid type:</b><br />
<select onchange="changeGridType(this);">
<option value="circle">Circle</option>
<option selected="selected" value="hexagon">Hexagon</option>
<option value="hexCircle">Hex Circle</option>
<option value="pointyHexagon">Pointy Hexagon</option>
<option value="square">Square</option>
<option value="triangle">Triangle</option>
</select>
</div>
<fieldset style="width:calc(100% - 30px);min-width:290px;margin-top:10px;">
<legend><h1 style="font-size:16px">Extruded gridded data source</h1></legend>
This sample shows how to create a 3D polygon extrusions from a gridded data source.
Change the grid type to see the data be re-aggregated into a different grid system.
The data in this sample consists of 86,576 data points that represent each address in Fort Collins, Colorado.
Once downloaded, the data points are aggregated into cells of a grid very quickly.
Each cell is a hexgaon that is 0.25 miles wide.
This samples uses the open source <a href="https://github.com/Azure-Samples/azure-maps-gridded-data-source" target="_blank">Azure Maps Gridded Data Source module</a>.
</fieldset>
</body>
</html>