Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
139 lines (114 sloc) 5.61 KB
<!DOCTYPE html>
<html lang="en">
<head>
<title>Get current weather at a location - 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 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://atlas.azure.us/weather/currentConditions/json?api-version=1.0&subscription-key={subscription-key}&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>' +
'Feels like: {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 your Azure Maps subscription key to the map SDK. Get an Azure Maps key at https://azure.com/maps
authOptions: {
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('{subscription-key}', atlas.getSubscriptionKey()).replace('{query}', e.position[1] + ',' + e.position[0]);
fetch(requestUrl)
.then(function (response) {
return response.json();
}).then(function (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);
});
}
</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>
You can’t perform that action at this time.