Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
92 lines (76 sloc) 4.22 KB
<!DOCTYPE html>
<html>
<head>
<title>Polygon Hover Style - Azure Maps Web SDK Samples</title>
<meta charset="utf-8" />
<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="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="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;
var stateBoundaries = '../Common/data/geojson/US_States_500k.json';
function GetMap() {
//Initialize a map instance.
map = new atlas.Map('myMap', {
center: [-110, 50],
zoom: 2,
view: 'Auto',
//Add your Azure Maps subscription key to the map SDK. Get an Azure Maps key at https://azure.com/maps
authOptions: {
authType: 'subscriptionKey',
subscriptionKey: '<Your Azure Maps Key>'
}
});
//Wait until the map resources are ready.
map.events.add('ready', function () {
//Create a data source and add it to the map.
datasource = new atlas.source.DataSource();
map.sources.add(datasource);
//Load the US state boundary data.
datasource.importDataFromUrl(stateBoundaries);
//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.
map.events.add('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.
map.events.add('mouseleave', polygonLayer, function (e) {
polygonHoverLayer.setOptions({ filter: ['==', ['get', 'GEO_ID'], ''] });
});
});
}
</script>
</head>
<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>Polygon Hover Style</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.
</fieldset>
</body>
</html>
You can’t perform that action at this time.