Skip to content

Commit 4a94242

Browse files
committed
more cors info + js example; more maps info
1 parent add1f26 commit 4a94242

File tree

9 files changed

+122
-6
lines changed

9 files changed

+122
-6
lines changed

doc/WebClients.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,25 @@ summary: >
1515
Here are some tips on writing browser-based clients for the Weather API. This includes
1616
frameworks like React and Vue, but also plain Javascript and older technologies like
1717
AJAX/XmlHpptRequest. Even inline images ("hotlinking") is to some degree supported.
18+
Note that you can get still throttled if you site sends > 20 reqs/sec (total from
19+
all current users' browsers).
1820

21+
## Example
22+
23+
This is about as plain an example as you can get with pure Javascript with no
24+
jQuery, TypeScript, React or other dependencies.
25+
26+
<script src="./assets/getForecast.js"></script>
27+
<form name="coords" action="javascript:getForecast()">
28+
<label for="lat">Latitude</label>
29+
<input type="text" id="lat" size="6" name="lat">&nbsp;&nbsp;
30+
<label for="lon">Longitude:</label>
31+
<input type="text" id="lon" size="6" name="lon"><br><br>
32+
<input type="submit" value="Submit"> <br><br>
33+
<textarea id="output" cols="60" rows="25" style="font-family: monospace, monospace"></textarea>
34+
</form>
35+
36+
See [the script](./assets/getForecast.js) for source code.
1937

2038
## Identification
2139

@@ -69,6 +87,13 @@ which are:
6987
- `Viewport-Width`
7088
- `Width`
7189

90+
*([Some sources](https://javascript.info/fetch-crossorigin) claim the list is even shorter.)
91+
92+
{: .note }
93+
Apparently some browsers now have added `User-Agent` to the list of "safe" headers. However,
94+
according to our research, this does not include Firefox (as of v.136.0) which triggers a CORS fault.
95+
Also, while Chrome appears to accept setting the header, it is not actually sent to the server.
96+
7297
3. The only allowed values for the `Content-Type` header are:
7398

7499
- `application/x-www-form-urlencoded`

doc/assets/getForecast.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function getForecast() {
2+
let lat = document.coords.lat.value;
3+
let lon = document.coords.lon.value;
4+
fetch_json('https://api.met.no/weatherapi/locationforecast/2.0/compact.json?lat=' + lat + '&lon=' + lon).then(
5+
function(value) {
6+
if (value != null) {
7+
document.getElementById('output').value = JSON.stringify(value,null,2);
8+
} else {
9+
document.getElementById('output').value = 'Failure';
10+
}
11+
}
12+
);
13+
}
14+
15+
async function fetch_json(url) {
16+
let obj = null;
17+
try {
18+
obj = await (await fetch(url)).json();
19+
} catch(e) {
20+
console.log(e, url);
21+
}
22+
return(obj);
23+
}
24+
25+
// Usage in HTML
26+
//
27+
//<script src="./assets/getForecast.js"></script>
28+
//<form name="coords" action="javascript:getForecast()">
29+
// <label for="lat">Latitude</label>
30+
// <input type="text" id="lat" size="6" name="lat">&nbsp;&nbsp;
31+
// <label for="lon">Longitude:</label>
32+
// <input type="text" id="lon" size="6" name="lon"><br><br>
33+
// <input type="submit" value="Submit"> <br><br>
34+
// <textarea id="output" cols="60" rows="25" style="font-family: monospace, monospace"></textarea>
35+
//</form>

doc/index.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ Helpdesk's greatest hits
5858

5959
## Other data sources
6060

61-
- [Map client and geolocation libraries](./MapClients)
62-
- [WMS services](./WMS) (Under construction)
61+
- [Map client and geolocation libraries](./maps/MapClients)
62+
- [WMS services](./mapsWMS) (Under construction)
6363
- [Fetching gridded forecast data from THREDDS](./thredds)
6464

6565
## Backend service development
@@ -73,3 +73,6 @@ For MET employees only
7373
## Support
7474

7575
- [News and contact information](./support)
76+
77+
*Content generated using [Jekyll](https://jekyllrb.com/) and
78+
[Just the Docs](https://github.com/just-the-docs/just-the-docs).*

doc/maps.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: Maps
3+
layout: page
4+
nav_order: 55
5+
has_children: true
6+
---
7+
8+
## Yo, we got maps, bro
9+
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Map libraries for WeatherAPI
33
date: 2020-08-18
44
author: Geir Aalberg
55
layout: page
6-
parent: Other data sources
6+
parent: Maps
77
state: draft
88
tags:
99
- reference

doc/WMS.md renamed to doc/maps/WMS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Introduction to WMS
33
date: 2020-08-23
44
author: Geir Aalberg
55
layout: page
6-
parent: Other data sources
6+
parent: Maps
77
#nav_exclude: true
88
state: draft
99
tags:

doc/maps/yr-maps.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
title: Yr-maps
3+
date: 2025-03-12
4+
author: Geir Aalberg
5+
layout: page
6+
parent: Maps
7+
nav_exclude: true
8+
state: draft
9+
tags:
10+
- guide
11+
summary: >
12+
Getting started with Yr-maps
13+
---
14+
15+
## Demo
16+
17+
18+
<script src="https://unpkg.com/maplibre-gl/dist/maplibre-gl.js"></script>
19+
<link href="https://unpkg.com/maplibre-gl/dist/maplibre-gl.css" rel="stylesheet" />
20+
21+
<div id='map' style="width: 100%; height: 800px;"></div>
22+
<script>
23+
var map = new maplibregl.Map({
24+
container: 'map',
25+
style: 'http://157.249.72.12/styles/basic-preview/style.json',
26+
center: [15, 65],
27+
zoom: 4
28+
});
29+
</script>
30+
31+
32+
## Datakilder
33+
34+
- <https://beta.yr-maps.met.no/> - kart-API med dokumentasjon
35+
36+
Kartilene bruker WebMercator-projeksjon, som er default i de fleste kartklienter.
37+
38+
## Ressurser
39+
40+
### Yr-maps
41+
42+
- [Dokumentasjon fra NRK](https://nrkno.github.io/yr-map-docs/) på tile rendering med [kildekode](https://github.com/nrkno/yr-map-docs)
43+
- [TileConvert](https://github.com/metno/tileconvert) - Go-kode for proxy server for produksjon av tiles som kan leses direkte inn i kartbibliotek
44+
- [Yr-maps demo client](https://client.yr-maps.met.no/)

doc/thredds.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ For more information, please see the section on "Data Access" in the [Numerical
1919
Weather Prediction wiki](https://github.com/metno/NWPdocs/wiki/Data-access).
2020

2121
THREDDS also supports the Web Map Service protocol for interactive raster images.
22-
For a quick guide, see the [WMS page](./WMS).
22+
For a quick guide, see the [WMS page](./maps/WMS).
2323

2424
## OPeNDAP
2525

site

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ abort() {
1818
case "$action" in
1919

2020
run)
21-
bundle exec jekyll serve
21+
bundle exec jekyll serve &
2222
;;
2323

2424
post)

0 commit comments

Comments
 (0)