Windy Map Forecast API
repository·master·Indexed 21 days ago
https://github.com/windycom/apiA library built on Leaflet 1.4.x that allows developers to embed Windy.com's weather visualizations into web applications. The API provides tools for managing map parameters via windyAPI.store, handling weather data through a broadcast system, customizing the UI with CSS, and utilizing a weather picker to retrieve and convert raw meteorological data.
What's inside windycom-api
- The Map Forecast API is a library built on top of Leaflet 1.4.x. It enables developers to integrate Windy.com's map visualizations into their own applications. Because it is based on Leaflet, you can combine standard Leaflet functionality and JavaScript with Windy's specialized weather layers.
Use the broadcast system to observe API events
masterThe Windy API uses a broadcast system to emit events. You can listen for, remove, or trigger messages using the standardon,off,once, andfiremethods provided bywindyApi.broadcast.Integrate Windy with Leaflet
masterThe Windy API is built on top of Leaflet. Once the Windy API is initialized, you can use the standard Leaflet API or any compatible Leaflet plugins to manipulate the map, add layers, or handle user interactions.How to change Windy parameters at start-up vs runtime
masterYou can control the initial state of the Windy map in two ways:
- Start-up: Pass an
optionsobject during initialization. This is recommended for performance as it can lead to faster loading times. - Runtime: Use
windyAPI.store.set(key, value)to change parameters while the map is already running.
Parameters are identified by string identifiers (e.g.,
overlaycan be set torainAccu, orlevelcan be set to850h).- Start-up: Pass an
Enable verbose logging for broadcasts
masterTo see detailed, color-coded broadcast information in the browser's console, include
verbose: truein your startupoptionsobject when initializing the Windy API.// Example startup configuration const windyApi = new WindyApi({ // ... other options verbose: true });Explore Map Forecast API examples
masterThe repository provides several practical examples to help you get started with different features:
- Hello World: Basic initialization.
- Change Windy parameters: How to manipulate weather layer settings.
- Observe what is happening inside: Deep dive into internal processes.
- Mess with User Interface: Customizing navigation and UI elements.
- Change units: Managing different measurement metrics.
- Use weather picker: Implementing a selector for weather variables.
- Boat tracker: Specialized implementation for tracking vessels.
Trigger tasks after data rendering with redrawFinished
masterTheredrawFinishedbroadcast is triggered once Windy has successfully loaded and rendered the requested data. This is the recommended event to use for triggering your own custom tasks or UI updates to ensure the data is ready.Customize the Windy map user interface with CSS
masterYou can customize the appearance of the Windy map user interface by applying custom CSS. All UI elements are contained within the
#windyselector.Important Constraint: When applying styles, ensure that the Windy logo remains:
- Unscaled
- Clickable
- With an opacity of
1.
/* Example of targeting the Windy UI container */ #windy { /* your custom styles here */ } /* Ensure the logo is not broken by your styles */ #windy .windy-logo { transform: none !important; pointer-events: auto !important; opacity: 1 !important; }Initialize the Windy Map with windyInit()
masterTo load the Windy Map, you must first include the Leaflet library and then the Windy API library via the following URL:
https://api.windy.com/assets/map-forecast/libBoot.js.Your HTML must include a
<div id="windy"></div>element to serve as the map container. You can use CSS to control the size and position of this container.In your JavaScript, call
windyInit(options, callback)to start the API. Theoptionsobject requires akeyproperty containing your API key. Thecallbackfunction is executed once the API is ready, receiving the Windy API instance as its argument.<!-- 1. HTML Container --> <div id="windy"></div> <!-- 2. Load Libraries --> <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" /> <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script> <script src="https://api.windy.com/assets/map-forecast/libBoot.js"></script> <!-- 3. Initialize --> <script> windyInit({ key: 'YOUR_API_KEY' }, function(windy) { console.log('Windy API is ready:', windy); // Use the 'windy' object to interact with the map }); </script>Change units for an overlay metric
masterEach Windy overlay is accessible via the
windyApi.overlaysmodule. To manage the units (metrics) of a specific overlay, use the corresponding overlay object.For example, when working with the
windoverlay:- Use
overlays.wind.metric(read-only) to retrieve the current metric. - Use
overlays.wind.listMetrics()to retrieve a list of all allowed metrics for that overlay. - Use
overlays.wind.setMetric(metric)to update the overlay to a new metric.
// Get the current metric for wind const currentMetric = windyApi.overlays.wind.metric; // Get all available metrics for wind const availableMetrics = windyApi.overlays.wind.listMetrics(); // Set the wind metric to a new value windyApi.overlays.wind.setMetric('new_metric_value');- Use
Update the favOverlays list
masterWhen modifying the
favOverlaysparameter, you must always provide a new array instance. BecausewindyAPI.storeuses primitive comparison to detect changes, passing the same array reference will not trigger an update.// Correct way to add an overlay to favorites const currentFavs = windyAPI.store.get('favOverlays'); windyAPI.store.set('favOverlays', [...currentFavs, 'newOverlayId']);Implement a boat tracker using Leaflet and GeoJSON
masterTo build a tracker for boats, planes, or cars, you should create a backend that serves GeoJSON tracks. The frontend implementation relies on the Leaflet API to handle map rendering and data visualization. You can use Leaflet to consume your GeoJSON endpoints and display the movement of entities on a map.