Skip to content

Commit 7052b37

Browse files
prushforprushforth
authored andcommitted
Add comments, function for link behaviour, projection changes. Re-order
post-projection change sequence so that history is correctly initialized, hopefully. Update to do list
1 parent 1faeec0 commit 7052b37

File tree

4 files changed

+53
-34
lines changed

4 files changed

+53
-34
lines changed

src/mapml-viewer.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,11 @@ export class MapViewer extends HTMLElement {
349349
let lat = this.lat;
350350
let lon = this.lon;
351351
let zoom = this.zoom;
352+
// saving the lat, lon and zoom is necessary because Leaflet seems
353+
// to try to compensate for the change in the scales for each zoom
354+
// level in the crs by changing the zoom level of the map when
355+
// you set the map crs. So, we save the current view for use below
356+
// when all the layers' reconnections have settled.
352357
this._map.options.crs = M[newValue];
353358
this._map.options.projection = newValue;
354359
let layersReady = [];
@@ -360,12 +365,11 @@ export class MapViewer extends HTMLElement {
360365
layersReady.push(reAttach.whenReady());
361366
}
362367
Promise.allSettled(layersReady).then(() => {
363-
// if don't do a zoomTo, the BNG experiment ends up at zoom=5 (incorrectly)
364-
// BUT the feature link from Canada/BC to BCTILE layer works
365-
// can have one but not both tbd
366-
//
367-
this._resetHistory();
368+
// use the saved map location to ensure it is correct after
369+
// changing the map CRS. Specifically affects projection
370+
// upgrades, e.g. https://maps4html.org/experiments/custom-projections/BNG/
368371
this.zoomTo(lat, lon, zoom);
372+
this._resetHistory();
369373
this._map.announceMovement.enable();
370374
});
371375
}

src/mapml/utils/Util.js

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,6 @@ export var Util = {
496496
if (['/', '.', '#'].includes(link.url[0])) link.target = '_self';
497497
}
498498
if (!justPan) {
499-
let newLayer = false;
500499
layer = document.createElement('layer-');
501500
layer.setAttribute('src', link.url);
502501
layer.setAttribute('checked', '');
@@ -505,18 +504,12 @@ export var Util = {
505504
if (link.type === 'text/html') {
506505
window.open(link.url);
507506
} else {
508-
newLayer = true;
509-
if (!link.inPlace && zoomTo) {
510-
updateMapZoomTo(zoomTo);
511-
}
507+
postTraversalSetup();
512508
map.options.mapEl.appendChild(layer);
513509
}
514510
break;
515511
case '_parent':
516-
newLayer = true;
517-
if (!link.inPlace && zoomTo) {
518-
updateMapZoomTo(zoomTo);
519-
}
512+
postTraversalSetup();
520513
for (let l of map.options.mapEl.querySelectorAll('layer-'))
521514
if (l._layer !== leafletLayer) map.options.mapEl.removeChild(l);
522515
map.options.mapEl.appendChild(layer);
@@ -526,25 +519,11 @@ export var Util = {
526519
window.location.href = link.url;
527520
break;
528521
default:
529-
newLayer = true;
530-
if (!link.inPlace && zoomTo) {
531-
updateMapZoomTo(zoomTo);
532-
}
522+
postTraversalSetup();
533523
opacity = leafletLayer._layerEl.opacity;
534524
leafletLayer._layerEl.insertAdjacentElement('beforebegin', layer);
535525
map.options.mapEl.removeChild(leafletLayer._layerEl);
536526
}
537-
if (!link.inPlace && newLayer)
538-
layer.whenReady().then(() => {
539-
if (!layer.extent) {
540-
layer._layer._setLayerElExtent();
541-
}
542-
if (zoomTo)
543-
layer.parentElement.zoomTo(+zoomTo.lat, +zoomTo.lng, +zoomTo.z);
544-
else layer.zoomTo();
545-
if (opacity) layer.opacity = opacity;
546-
map.getContainer().focus();
547-
});
548527
} else if (zoomTo && !link.inPlace && justPan) {
549528
leafletLayer._map.options.mapEl.zoomTo(
550529
+zoomTo.lat,
@@ -554,7 +533,38 @@ export var Util = {
554533
if (opacity) layer.opacity = opacity;
555534
}
556535

536+
function postTraversalSetup() {
537+
// when the projection is changed as part of the link traversal process,
538+
// it's necessary to set the map viewer's lat, lon and zoom NOW, so that
539+
// the promises that are created when the viewer's projection is changed
540+
// can use the viewer's lat, lon and zoom properties that were in effect
541+
// before the projection change i.e. in the closure for that code
542+
// see mapml-viewer / map is=web-map projection attributeChangedCallback
543+
// specifically required for use cases like changing projection after
544+
// link traversal, e.g. BC link here https://maps4html.org/experiments/linking/features/
545+
if (!link.inPlace && zoomTo) updateMapZoomTo(zoomTo);
546+
// the layer is newly created, so have to wait until it's fully init'd
547+
// before setting properties.
548+
layer.whenReady().then(() => {
549+
// TODO refactor _setLayerElExtent so that it's invoked automatically
550+
// by layer.extent getter TBD.
551+
if (!layer.extent) {
552+
layer._layer._setLayerElExtent();
553+
}
554+
// if the map projection isnt' changed by link traversal, it's necessary
555+
// to perform pan/zoom operations after the layer is ready
556+
if (!link.inPlace && zoomTo)
557+
layer.parentElement.zoomTo(+zoomTo.lat, +zoomTo.lng, +zoomTo.z);
558+
else if (!link.inPlace) layer.zoomTo();
559+
// not sure if this is necessary
560+
if (opacity) layer.opacity = opacity;
561+
// this is necessary to display the FeatureIndexOverlay, I believe
562+
map.getContainer().focus();
563+
});
564+
}
565+
557566
function updateMapZoomTo(zoomTo) {
567+
// can't use mapEl.zoomTo(...) here, it's too slow!
558568
map.options.mapEl.lat = +zoomTo.lat;
559569
map.options.mapEl.lon = +zoomTo.lng;
560570
map.options.mapEl.zoom = +zoomTo.z;

src/web-map.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,18 +394,28 @@ export class WebMap extends HTMLMapElement {
394394
let lat = this.lat;
395395
let lon = this.lon;
396396
let zoom = this.zoom;
397+
// saving the lat, lon and zoom is necessary because Leaflet seems
398+
// to try to compensate for the change in the scales for each zoom
399+
// level in the crs by changing the zoom level of the map when
400+
// you set the map crs. So, we save the current view for use below
401+
// when all the layers' reconnections have settled.
397402
this._map.options.crs = M[newValue];
398403
this._map.options.projection = newValue;
399404
let layersReady = [];
405+
this._map.announceMovement.disable();
400406
for (let layer of this.querySelectorAll('layer-')) {
401407
layer.removeAttribute('disabled');
402408
let reAttach = this.removeChild(layer);
403409
this.appendChild(reAttach);
404410
layersReady.push(reAttach.whenReady());
405411
}
406412
Promise.allSettled(layersReady).then(() => {
413+
// use the saved map location to ensure it is correct after
414+
// changing the map CRS. Specifically affects projection
415+
// upgrades, e.g. https://maps4html.org/experiments/custom-projections/BNG/
407416
this.zoomTo(lat, lon, zoom);
408417
this._resetHistory();
418+
this._map.announceMovement.enable();
409419
});
410420
}
411421
};

todo-tests

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
TODO
22

3-
4-
experiments/linking/features - clicking on BC does not work, Ontario link not working
5-
experiments/vector tiles - not working
6-
experiments/custom-projections/Arctic-SDI/ Canada link not working
7-
83
deal with flaky tests???
94

105
core/featureIndexOverlay test is broken, integrate other PR

0 commit comments

Comments
 (0)