Weather wind show direction arrow

Witness the awe-inspiring display of the weather wind layer in MapTiler Weather Wind dataset. Explore the wind speed and direction at any desired location by simply following the cursor. Immerse yourself in the captivating wind animation, boasting a remarkable 60 frames per second.

NPM module setup


npm install --save @maptiler/sdk @maptiler/weather
import { Map, MapStyle, config } from '@maptiler/sdk';
import '@maptiler/sdk/dist/maptiler-sdk.css';
import { WindLayer } from '@maptiler/weather';

const timeInfoContainer = document.getElementById("time-info");
const timeTextDiv = document.getElementById("time-text");
const timeSlider = document.getElementById("time-slider");
const playPauseButton = document.getElementById("play-pause-bt");
const pointerDataDiv = document.getElementById("pointer-data");
let pointerLngLat = null;

config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';

const map = new Map({
  container: document.getElementById('map'),
  hash: true,
  zoom: 2,
  center: [0, 40],
  style: MapStyle.BACKDROP,
  projectionControl: true
});

const layer = new WindLayer();

map.on('load', function () {
  // Darkening the water layer to make the land stand out
  map.setPaintProperty("Water", 'fill-color', "rgba(0, 0, 0, 0.4)");
  map.addLayer(layer, 'Water');
});

timeSlider.addEventListener("input", (evt) => {
  layer.setAnimationTime(parseInt(timeSlider.value / 1000))
})

// Event called when all the datasource for the next days are added and ready.
// From now on, the layer nows the start and end dates.
layer.on("sourceReady", event => {
  const startDate = layer.getAnimationStartDate();
  const endDate = layer.getAnimationEndDate();
  const currentDate = layer.getAnimationTimeDate();
  refreshTime()

  timeSlider.min = +startDate;
  timeSlider.max = +endDate;
  timeSlider.value = +currentDate;
})

// Called when the animation is progressing
layer.on("tick", event => {
  refreshTime();
  updatePointerValue(pointerLngLat);
})

// Called when the time is manually set
layer.on("animationTimeSet", event => {
  refreshTime()
})

// When clicking on the play/pause
let isPlaying = false;
playPauseButton.addEventListener("click", () => {
  if (isPlaying) {
    layer.animateByFactor(0);
    playPauseButton.innerText = "Play 3600x";
  } else {
    layer.animateByFactor(3600);
    playPauseButton.innerText = "Pause";
  }

  isPlaying = !isPlaying;
})

// Update the date time display
function refreshTime() {
  const d = layer.getAnimationTimeDate();
  timeTextDiv.innerText = d.toString();
  timeSlider.value = +d;
}

function updatePointerValue(lngLat) {
  if (!lngLat) return;
  pointerLngLat = lngLat;
  const value = layer.pickAt(lngLat.lng, lngLat.lat);
  if (!value) {
    pointerDataDiv.innerText = "";
    return;
  }
  pointerDataDiv.innerHTML = `<div id="arrow" style="transform: rotate(${value.directionAngle}deg);">↑</div>
  ${value.compassDirection} ${value.speedKilometersPerHour.toFixed(1)} km/h`;
}

timeInfoContainer.addEventListener("mouseenter", () => {
  pointerDataDiv.innerText = "";
})

map.on('mousemove', (e) => {
  updatePointerValue(e.lngLat);
});

Learn more

Check out the Weather JS module reference

Check out the Wind layer reference

Weather custom popup

Weather custom popup

Examples

Create highly customizable popups for weather maps using MarkerLayout.

Weather layer switcher

Weather layer switcher

Examples

Switch weather layers and view detailed data under cursors.

Weather wind and temperature layer

Weather wind and temperature layer

Examples

Visualize wind and temperature layers with cursor data.

Weather Plus wind arrow layer

Weather+ wind arrow layer

Examples

Visualize wind direction and speed using animated wind arrows.

Was this helpful?