Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
<!DOCTYPE html>
<html lang="en">
<head>
<title>Limit Scroll Wheel Zoom - 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 limit the scroll zooming functionality of the map such that the map will only be zoomed when the CTRL key is pressed. When the CTRL key is not pressed, the scroll wheel will scroll the page when over the map." />
<meta name="keywords" content="Microsoft maps, map, gis, API, SDK, mouse wheel, mousewheel, scroll page, map, user interaction, scrollZoomInteraction" />
<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;
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', {
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>'
}
});
//Limit the scroll wheel zooming such that the map is only zoomed when the CTRL key is pressed.
LimitScrollWheelZoom(map);
}
function LimitScrollWheelZoom(map) {
map.setUserInteraction({ scrollZoomInteraction: false });
//Create a message dialog to tell the user how to zoom the map if they use the scroll wheel on the map without CTRL.
var msgDialog = document.createElement('div');
Object.assign(msgDialong.style, {
display: 'none',
position: 'absolute',
top: 0,
left: 0,
width: '100%',
height: '100%',
backgroundColor: 'rgba(0, 0, 0, 0.5)',
color: 'white',
fontSize: '24px',
fontWeight: 'bold',
transition: 'visibility 0s linear 0s opacity 500ms'
});
msgDialog.innerHTML = '<div style="position:relative;float:left;top:50%;left:50%;transform:translate(-50%,-50%);">Use ctrl + scroll to zoom the map</div>'
map.getMapContainer().append(msgDialog);
//Show the message dialog for 2 seconds then fade out over 300ms.
var showMsgDialog = function () {
msgDialog.style.display = '';
msgDialog.style.opacity = 1;
setTimeout(function () {
msgDialog.style.opacity = 0;
setTimeout(function () {
msgDialog.style.display = 'none';
}, 300);
}, 2000);
};
map.getMapContainer().addEventListener('mousewheel', function (e) {
if (!e.ctrlKey) {
showMsgDialog();
}
});
window.addEventListener('keydown', function (e) {
if (e.ctrlKey) {
map.setUserInteraction({ scrollZoomInteraction: true });
}
});
window.addEventListener('keyup', function (e) {
if (!e.ctrlKey) {
map.setUserInteraction({ scrollZoomInteraction: false });
}
});
}
</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><h1 style="font-size:16px">Limit Scroll Wheel Zoom</h1></legend>
This sample shows how to limit the scroll zooming functionality of the map such that the map will only be zoomed when the CTRL key is pressed.
When the CTRL key is not pressed, the scroll wheel will scroll the page when over the map.
</fieldset>
<br /><br />Adding several new lines so that the page can be scrolled.<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
</body>
</html>