Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
125 lines (102 sloc) 5.41 KB
<!DOCTYPE html>
<html lang="en">
<title>Symbol image selection state - 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 change the image of a symbol based on its selection state. " />
<meta name="keywords" content="Microsoft maps, map, gis, API, SDK, markers, pins, pushpins, symbols, layer, icon, image" />
<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, selectedShapeId;
function GetMap() {
//Initialize a map instance.
map = new atlas.Map('myMap', {
zoom: 4,
view: 'Auto',
//Add your Azure Maps subscription key to the map SDK. Get an Azure Maps key at
authOptions: {
authType: 'subscriptionKey',
subscriptionKey: '<Your Azure Maps Key>'
//Wait until the map resources are ready.'ready', function () {
//Load two custom image icons into the map resources.
var images = [
map.imageSprite.add('inactive-icon', '../Common/images/icons/purple_pin_small.png'),
map.imageSprite.add('active-icon', '../Common/images/icons/purple_pin.png'),
Promise.all(images).then(function () {
//Create a data source and add it to the map.
datasource = new atlas.source.DataSource();
//Create point features and add it to the data source.
new[0, 0])),
new[-10, 0])),
new[10, 0]))
//Add a layer for rendering point data as symbols.
var layer = new atlas.layer.SymbolLayer(datasource, null, {
iconOptions: {
//Check the icon property of the feature to set the image.
image: [
['has', 'icon'],
['get', 'icon'],
//Default to the inactive icon if no icon property set on feature.
//Add layer to map.
//Add click event to layer.'click', layer, symbolClicked);
//When the mouse is over the layer, change the cursor to be a pointer.'mouseover', layer, function () {
map.getCanvasContainer().style.cursor = 'pointer';
//When the mouse leaves the item on the layer, change the cursor back to the default which is grab.'mouseout', layer, function () {
map.getCanvasContainer().style.cursor = 'grab';
function symbolClicked(e) {
//Get the clicked shape an it's id.
var shape = e.shapes[0];
var shapeId = e.shapes[0].getId();
var properties = shape.getProperties();
//If the shape is already showing the active icon, toggle it to show the inactive icon.
icon: (properties.icon === 'active-icon') ? 'inactive-icon' : 'active-icon'
//If a shape has previously been selected, revert it's icon to the inactive icon.
if (selectedShapeId) {
var s = datasource.getShapeById(selectedShapeId);
icon: 'inactive-icon'
//If the shape id is new, cache it, otherwise no shape is active, cache null.
selectedShapeId = (selectedShapeId !== shapeId) ? shapeId : null;
<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">Symbol image selection state</h1></legend>
This sample shows how to change the image of a symbol based on its selection state.
This sample looks at all features in a data source and only allows one feature to be active.
Clicking on an active feature will toggle it to its inactive state.
You can’t perform that action at this time.