Permalink
Cannot retrieve contributors at this time
AzureMapsGovCloudCodeSamples/AzureMapsCodeSamples/Symbol Layer/Filter Symbols by Property.html
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
119 lines (103 sloc)
6.42 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>Filter Symbols by Property - 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 filter symbols on the map by property by creating a layer fro each property value and then toggling the visibility of that layer accordingly. " /> | |
<meta name="keywords" content="Microsoft maps, map, gis, API, SDK, symbols, markers, pins, pushpins, styling, style, layer" /> | |
<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, client, datasource, layer; | |
var categories = ['bar', 'coffee', 'restaurant']; | |
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: [-122.338124, 47.6078], | |
zoom: 16, | |
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>' | |
} | |
}); | |
//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); | |
//Add a data set to the data source. | |
datasource.add([ | |
new atlas.data.Feature(new atlas.data.Point([-122.338913, 47.607471]), { category: 'bar' }), | |
new atlas.data.Feature(new atlas.data.Point([-122.341187, 47.608192]), { category: 'bar' }), | |
new atlas.data.Feature(new atlas.data.Point([-122.335014, 47.607960]), { category: 'bar' }), | |
new atlas.data.Feature(new atlas.data.Point([-122.337555, 47.608620]), { category: 'bar' }), | |
new atlas.data.Feature(new atlas.data.Point([-122.338524, 47.606907]), { category: 'coffee' }), | |
new atlas.data.Feature(new atlas.data.Point([-122.336655, 47.606251]), { category: 'coffee' }), | |
new atlas.data.Feature(new atlas.data.Point([-122.336182, 47.607185]), { category: 'coffee' }), | |
new atlas.data.Feature(new atlas.data.Point([-122.337784, 47.607784]), { category: 'coffee' }), | |
new atlas.data.Feature(new atlas.data.Point([-122.338455, 47.606880]), { category: 'restaurant' }), | |
new atlas.data.Feature(new atlas.data.Point([-122.336823, 47.607239]), { category: 'restaurant' }), | |
new atlas.data.Feature(new atlas.data.Point([-122.339027, 47.608040]), { category: 'restaurant' }), | |
new atlas.data.Feature(new atlas.data.Point([-122.335892, 47.607594]), { category: 'restaurant' }) | |
]); | |
//Create a symbol layer to render the points. | |
layer = new atlas.layer.SymbolLayer(datasource, null, { | |
iconOptions: { | |
//The map control has built in icons for bar, coffee and restaurant that we can use. | |
image: ['get', 'category'], | |
anchor: 'center', | |
allowOverlap: true | |
} | |
}); | |
map.layers.add(layer); | |
}); | |
} | |
function filterSymbols(elm, category) { | |
//Set the visibility of the layer. | |
if (elm.checked) { | |
//Add the category to the categories array. | |
categories.push(category); | |
} else { | |
//Remove the category to the categories array. | |
categories.splice(categories.indexOf(category), 1); | |
} | |
//Create a filter that grabs the category of each point and checks that it is in the array of categories. | |
var filter = ['in', ['get', 'category'], ['literal', categories]]; | |
//Update the filter in the layer. | |
layer.setOptions({ | |
filter: filter | |
}); | |
} | |
</script> | |
</head> | |
<body onload="GetMap()"> | |
<div id="myMap" style="position:relative;width:100%;min-width:290px;height:600px;"></div> | |
<div style="position:absolute;top:15px;left:15px;background-color:white;padding:10px;border-radius:10px;"> | |
<input type="checkbox" checked="checked" onclick="filterSymbols(this, 'bar')" /> Bar<br /> | |
<input type="checkbox" checked="checked" onclick="filterSymbols(this, 'coffee')" /> Coffee<br /> | |
<input type="checkbox" checked="checked" onclick="filterSymbols(this, 'restaurant')" /> Restaurant | |
</div> | |
<fieldset style="width:calc(100% - 30px);min-width:290px;margin-top:10px;"> | |
<legend><h1 style="font-size:16px">Filter Points by Symbols</h1></legend> | |
This sample shows how to filter symbols on the map by property by creating a layer fro each property value and then toggling the visibility of that layer accordingly. | |
</fieldset> | |
</body> | |
</html> |