Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
<!DOCTYPE html>
<html lang="en">
<head>
<title>Get current weather at a location - 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 retrieve weather data using the Azure Maps Current Conditions REST API." />
<meta name="keywords" content="Microsoft maps, map, gis, API, SDK, tiles, layer, weather, AccuWeather" />
<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, popup;
var currentConditionsUrl = 'https://{azMapsDomain}/weather/currentConditions/json?api-version=1.0&query={query}';
var weatherTemplate = {
//The title tag for the popup.
title: 'Current Conditions',
//HTML string template with placeholders for properties of the weather response.
content:
'<img class="weather-icon" src="/Common/images/icons/weather-black/{iconCode}.png"/>' +
'<div class="weather-content">' +
'<div class="weather-temp">{temperature/value}&#176;</div>' +
'RealFeel<sup>®</sup>: {realFeelTemperature/value}&#176;C' +
'<div class="weather-phrase">{phrase}</div>' +
'Humidity: {relativeHumidity}&#37</div>',
//Format numbers with two decimal places.
numberFormat: {
maximumFractionDigits: 2
},
//Since we trust the data being retrieve, don't sandbox the content so that we can use CSS classes.
sandboxContent: false
};
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: [0, 30],
zoom: 1,
view: 'Auto',
style: 'grayscale_dark',
//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);
//Create a popup but leave it closed so we can update it and display it later.
popup = new atlas.Popup();
//Add a click event to the map.
map.events.add('click', getWeatherForPoint);
});
}
function getWeatherForPoint(e) {
//Close the popup if it is open.
popup.close();
//Request the current conditions weather data and show it in the pop up.
var requestUrl = currentConditionsUrl.replace('{query}', e.position[1] + ',' + e.position[0]);
processRequest(requestUrl).then(response => {
var content;
if (response && response.results && response.results[0]) {
//Use the weatherTemplate settings to create templated content for the popup.
content = atlas.PopupTemplate.applyTemplate(response.results[0], weatherTemplate);
} else {
content = '<div style="padding:10px;">Weather data not available for this location.</div>';
}
popup.setOptions({
content: content,
position: e.position
});
popup.open(map);
});
}
function processRequest(url) {
//This is a reusable function that sets the Azure Maps platform domain, sings the request, and makes use of any transformRequest set on the map.
return new Promise((resolve, reject) => {
//Replace the domain placeholder to ensure the same Azure Maps cloud is used throughout the app.
url = url.replace('{azMapsDomain}', atlas.getDomain());
//Get the authentication details from the map for use in the request.
var requestParams = map.authentication.signRequest({ url: url });
//Transform the request.
var transform = map.getServiceOptions().tranformRequest;
if (transform) {
requestParams = transform(url);
}
fetch(requestParams.url, {
method: 'GET',
mode: 'cors',
headers: new Headers(requestParams.headers)
})
.then(r => r.json(), e => reject(e))
.then(r => {
resolve(r);
}, e => reject(e));
});
}
</script>
<style>
.weather-icon {
float: left;
}
.weather-content {
float: left;
margin: 0 10px;
}
.weather-temp {
font-size: 24px;
font-weight: bold;
}
.weather-phrase {
font-size: 16px;
font-weight: bold;
margin-top: 10px;
}
</style>
</head>
<body onload="GetMap()">
<div id="myMap" style="position:relative;width:100%;min-width:290px;height:600px;"></div>
<div style="position:absolute;top:10px;left:10px;padding:10px;background-color:white;border-radius:10px;">
Click anywhere on the map to retrieve current weather conditions.
</div>
<fieldset style="width:calc(100% - 30px);min-width:290px;margin-top:10px;">
<legend><h1 style="font-size:16px">Get current weather at a location</h1></legend>
This sample shows how to retrieve weather data using the <a href="https://docs.microsoft.com/rest/api/maps/weather/getcurrentconditionspreview" target="_blank">Azure Maps Current Conditions REST API</a>.
</fieldset>
</body>
</html>