Skip to content

Commit a28f78c

Browse files
committed
Add basic PWA geo protocol handler
1 parent 432fa57 commit a28f78c

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

app/assets/favicons/manifest.json.erb

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,11 @@
1414
"start_url": "/",
1515
"theme_color": "#7ebc6f",
1616
"background_color": "#fff",
17-
"display": "minimal-ui"
17+
"display": "minimal-ui",
18+
"protocol_handlers": [
19+
{
20+
"protocol": "geo",
21+
"url": "/?geouri=%s"
22+
}
23+
]
1824
}

app/assets/javascripts/osm.js.erb

+9
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,19 @@ OSM = {
6363

6464
mapParams: function (search) {
6565
const params = OSM.params(search),
66+
geoURI = URL.canParse(params.geouri) ? new URL(params.geouri) : {},
6667
mapParams = {};
6768

6869
if (params.mlon && params.mlat) {
6970
mapParams.marker = true;
7071
mapParams.mlon = parseFloat(params.mlon);
7172
mapParams.mlat = parseFloat(params.mlat);
7273
}
74+
if (geoURI.lon && geoURI.lat) {
75+
mapParams.marker = true;
76+
mapParams.mlon = parseFloat(geoURI.lon);
77+
mapParams.mlat = parseFloat(geoURI.lat);
78+
}
7379

7480
// Old-style object parameters; still in use for edit links e.g. /edit?way=1234
7581
for (const type of ["node", "way", "relation", "note"]) {
@@ -100,6 +106,9 @@ OSM = {
100106
mapParams.lon = params.mlon;
101107
mapParams.lat = params.mlat;
102108
mapParams.zoom = params.zoom || 12;
109+
} else if (geoURI.pathname) {
110+
[mapParams.lat, mapParams.lon] = geoURI.pathname.split(",");
111+
mapParams.zoom = geoURI.searchParams?.get("z") || Math.round(geoURI.pathname.replaceAll(/,?[-\d]+\./g, "").length / 0.6) - 2;
103112
} else if (loc) {
104113
[mapParams.lon, mapParams.lat, mapParams.zoom] = loc;
105114
} else if (OSM.home) {

test/javascripts/osm_test.js

+12
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ describe("OSM", function () {
8282
expect(params).to.have.property("zoom", 16);
8383
});
8484

85+
it("parses geoURIs", function () {
86+
let params = OSM.mapParams("?geouri=geo%3A57.6247%2C-3.6845");
87+
expect(params).to.have.property("lat", 57.6247);
88+
expect(params).to.have.property("lon", -3.6845);
89+
expect(params).to.have.property("zoom", 11);
90+
91+
params = OSM.mapParams("?geouri=geo%3A57.6247%2C-3.6845%3Fz%3D16");
92+
expect(params).to.have.property("lat", 57.6247);
93+
expect(params).to.have.property("lon", -3.6845);
94+
expect(params).to.have.property("zoom", 16);
95+
});
96+
8597
it("parses lat/lon/zoom from the hash", function () {
8698
location.hash = "#map=16/57.6247/-3.6845";
8799
const params = OSM.mapParams("?");

0 commit comments

Comments
 (0)