forked from windycom/windy-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.html
107 lines (93 loc) · 3.71 KB
/
plugin.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<plugin>
<!-- this Plugin does not have any HTML content -->
<script>
// Windy API modules are imported via '@windy/nameOfModule'
import map from '@windy/map';
import interpolator from '@windy/interpolator';
import _ from '@windy/utils';
import store from '@windy/store';
import bcast from '@windy/broadcast';
// List of wayponts to be interpolated
const points = [
['Tours', 'city-2', 0.689, 47.39],
['Le Mans', 'city-2', 0.2, 48.008],
['Amilly', 'city-3', 2.767, 47.978],
['Bourges', 'city-3', 2.399, 47.081],
['Paris', 'city-1', 2.351, 48.857],
['Chartres', 'city-2', 1.487, 48.447],
];
// List of current markers
let markers = null;
const icon = L.divIcon({
className: 'weather-at-city',
iconSize: [80, 40],
iconAnchor: [40, 20],
});
// Creates marker with content
const createPopup = (lat, lon, name) => {
const marker = L.marker([lat, lon], { icon }).addTo(map);
marker._icon.innerHTML = name;
return marker;
};
// Major interpolation function
const interpolateValues = () => {
/**
* This example can interpolate only wind overlay, but
* you can interpolate almost any raster layer of Windy
*/
if (store.get('overlay') !== 'wind') {
console.warn('I can iterpolate only Wind sorry');
return;
}
/**
* Interpolator returns interpolation function
*/
interpolator(interpolate => {
markers.forEach((m, i) => {
// eslint-disable-next-line no-unused-vars
const [name, cls, lon, lat] = points[i];
/**
* Interpolate finaly gets you the values
* @param {Object} { lat, lon }
* @return {Array} array of raw meterological values or null, NaN, -1
*/
const values = interpolate.call(interpolator, { lat, lon });
/**
* Remeber that we are able to interpolate values only for
* points that are visible on map
*/
if (Array.isArray(values)) {
const { wind, dir } = _.wind2obj(values);
m._icon.innerHTML = `${name}<br />${Math.round(
wind
)}m/s ${dir}`;
} else {
console.warn(
`Unable to interpolate value for ${lat}, ${lon}.`
);
}
});
});
};
this.onopen = () => {
map.setView({ lat: 47.3, lng: 2 }, 7);
// Lets change layer to Wind
store.set('overlay', 'wind');
if (!markers) {
markers = points.map(p => createPopup(p[3], p[2], p[0]));
// Values are interpolated from the data set, used
// for rendering weather layers, therefore remember to call
// interpolation anytime something happens on a map
bcast.on('redrawFinished', interpolateValues);
}
interpolateValues();
};
this.onclose = () => {
if (markers) {
markers.forEach(m => map.removeLayer(m));
bcast.off('redrawFinished', interpolateValues);
markers = null;
}
};
</script>
</plugin>