Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
181 lines (150 sloc) 7.61 KB
<!DOCTYPE html>
<title>Show popup on hover - 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 show a popup when the mouse hovers over or touches a shape." />
<meta name="keywords" content="map, gis, API, SDK, pins, symbols, pushpins, markers, infobox, infowindow, hover" />
<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, popup, symbolLayer;
//Define an HTML template for a custom popup content laypout.
var popupTemplate = '<div class="customInfobox"><div class="name">{name}</div>{description}</div>';
function GetMap() {
//Initialize a map instance.
map = new atlas.Map('myMap', {
center: [-122.33, 47.63],
zoom: 11,
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 () {
//Create a data source and add it to the map.
datasource = new atlas.source.DataSource();
//Create three point features on the map and add some metadata in the properties which we will want to display in a popup.
var point1 = new[-122.33, 47.6]), {
name: 'Point 1 Title',
description: 'This is the description 1.'
var point2 = new[-122.335, 47.645]), {
name: 'Point 2 Title',
description: 'This is the description 2.'
var point3 = new[-122.325, 47.635]), {
name: 'Point 3 Title',
description: 'This is the description 3.'
//Add the symbol to the data source.
datasource.add([point1, point2, point3]);
//Add a layer for rendering point data as symbols.
symbolLayer = new atlas.layer.SymbolLayer(datasource, null, { iconOptions: {allowOverlap: true}});
//Create a popup but leave it closed so we can update it and display it later.
popup = new atlas.Popup({
position: [0, 0],
pixelOffset: [0, -18]
* Open the popup on mouse move or touchstart on the symbol layer.
* Mouse move is used as mouseover only fires when the mouse initially goes over a symbol.
* If two symbols overlap, moving the mouse from one to the other won't trigger the event for the new shape as the mouse is still over the layer.
*/'mousemove', symbolLayer, symbolHovered);'touchstart', symbolLayer, symbolHovered);
//Close the popup on mouseout or touchend.'mouseout', symbolLayer, closePopup);'touchend', closePopup);
function closePopup() {
function symbolHovered(e) {
//Make sure the event occurred on a shape feature.
if (e.shapes && e.shapes.length > 0) {
//By default, show the popup where the mouse event occurred.
var pos = e.position;
var offset = [0, 0];
var properties;
if (e.shapes[0] instanceof atlas.Shape) {
properties = e.shapes[0].getProperties();
//If the shape is a point feature, show the popup at the points coordinate.
if (e.shapes[0].getType() === 'Point') {
pos = e.shapes[0].getCoordinates();
offset = [0, -18];
} else {
properties = e.shapes[0].properties;
//If the shape is a point feature, show the popup at the points coordinate.
if (e.shapes[0].type === 'Point') {
pos = e.shapes[0].geometry.coordinates;
offset = [0, -18];
//Update the content and position of the popup.
//Create a table from the properties in the feature.
content: '<div style="padding:10px;max-height:200px;overflow-y:auto;">' + object2Table(properties) + '</div>',
position: pos,
pixelOffset: offset
//Open the popup.;
function object2Table(obj) {
//Create a HTML table from an objects property names and values.
var html = ['<table><tr><td><b>Property</b></td><td><b>Value</b></td><tr>'];
Object.keys(obj).forEach(function (key, index) {
//Ignore private properties which are commonly denoted using an underscore.
if (key.indexOf('_') !== 0) {
html.push('<tr><td>', key, '</td><td>');
if (typeof obj[key] === 'object') {
//If the value of the property is an object, create a sub-table recursively.
} else {
return html.join('');
/* CSS styles used by custom popup template */
.customInfobox {
max-width: 200px;
padding: 10px;
font-size: 12px;
.customInfobox .name {
font-size: 14px;
font-weight: bold;
margin-bottom: 5px;
<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>Show popup on hover</legend>
This sample shows how to show a popup when the mouse hovers over or touches a shape.
This sample uses the mouse move event to detect when hovering over a shape instead of the mouse over event.
The mouse enter and over events only fires when initially hovering over a shape in a layer.
If two shapes overlap, these events won't fire again. The mouse move event will fire while moving the mouse over shapes in the layer.
You can’t perform that action at this time.