Weather by point
This example demonstrates how to display real-time weather information on a map using the Weather service. Click on any location on the map to view current weather conditions for that area.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdn.smartmaps.cloud/packages/smartmaps/weather/umd/weather.min.js"></script>
<script src="https://cdn.smartmaps.cloud/packages/smartmaps/smartmaps-gl/v2/umd/smartmaps-gl.min.js"></script>
</head>
<style>
body {
margin: 0;
padding: 0;
}
html,
body,
#map {
height: 100%;
}
</style>
<body>
<div id="map"></div>
<script>
const apiKey = 'INSERT API KEY';
const map = new smartmapsgl.Map({
apiKey: apiKey,
container: 'map',
center: { lat: 49.02164948779226, lng: 8.439330018049352 },
zoom: 10,
style: smartmapsgl.MapStyle.LIGHT
});
const weather = new smartmaps.weatherService.Weather(apiKey);
map.on('click', async (event) => {
const params = { latitude: event.lngLat.lat, longitude: event.lngLat.lng };
const weatherData = await weather.point(params);
const currentWeather = weatherData.features[0].properties.currently;
const temperature = Math.round(currentWeather.temperature2Meters);
const description = currentWeather.weather.description;
const rain = Math.round(currentWeather.rainTotal);
const humidity = Math.round(currentWeather.humidity);
const windSpeed = Math.round(currentWeather.wind10Meters);
const windDirection = Math.round(currentWeather.windDirection10Meters);
const cloudCover = Math.round(currentWeather.cloudCover);
const popupContent = `
<strong>Weather Information:</strong><br>
Temperature: ${temperature}°C<br>
Description: ${description}<br>
Rain: ${rain} mm<br>
Humidity: ${humidity}%<br>
Wind Speed: ${windSpeed} m/s<br>
Wind Direction: ${windDirection}°<br>
Cloud Cover: ${cloudCover}%<br>
`;
new smartmapsgl.Popup().setLngLat(event.lngLat).setHTML(popupContent).addTo(map);
});
</script>
</body>
</html>