MCPcopy Create free account
hub / github.com/Maps4HTML/MapML.js / reconnectLayers

Method reconnectLayers

src/web-map.js:440–489  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

438 break;
439 case 'projection':
440 const reconnectLayers = () => {
441 if (this._map && this._map.options.projection !== newValue) {
442 // save map location and zoom
443 let lat = this.lat;
444 let lon = this.lon;
445 let zoom = this.zoom;
446 // saving the lat, lon and zoom is necessary because Leaflet seems
447 // to try to compensate for the change in the scales for each zoom
448 // level in the crs by changing the zoom level of the map when
449 // you set the map crs. So, we save the current view for use below
450 // when all the layers' reconnections have settled.
451 // leaflet doesn't like this: https://github.com/Leaflet/Leaflet/issues/2553
452 this._map.options.crs = M[newValue];
453 this._map.options.projection = newValue;
454 let layersReady = [];
455 this._map.announceMovement.disable();
456 for (let layer of this.querySelectorAll('map-layer,layer-')) {
457 layer.removeAttribute('disabled');
458 let reAttach = this.removeChild(layer);
459 this.appendChild(reAttach);
460 layersReady.push(reAttach.whenReady());
461 }
462 return Promise.allSettled(layersReady).then(() => {
463 // For single-layer case (e.g. link traversal), synchronously set
464 // zoom constraints so that layer.zoomTo() uses correct bounds
465 const layers = this.querySelectorAll('map-layer,layer-');
466 if (layers.length === 1) {
467 const layer = layers[0];
468 if (layer.extent) {
469 this._map.setMinZoom(layer.extent.zoom.minZoom);
470 this._map.setMaxZoom(layer.extent.zoom.maxZoom);
471 }
472 }
473 // use the saved map location to ensure it is correct after
474 // changing the map CRS. Specifically affects projection
475 // upgrades, e.g. https://maps4html.org/experiments/custom-projections/BNG/
476 // see leaflet bug: https://github.com/Leaflet/Leaflet/issues/2553
477 this.zoomTo(lat, lon, zoom);
478 if (M.options.announceMovement)
479 this._map.announceMovement.enable();
480 // required to delay until map-extent.disabled is correctly set
481 // which happens as a result of map-layer._validateDisabled()
482 // which happens so much we have to delay until they calls are
483 // completed
484 setTimeout(() => {
485 this.dispatchEvent(new CustomEvent('map-projectionchange'));
486 }, 0);
487 });
488 }
489 };
490 if (
491 newValue &&
492 this._map &&

Callers

nothing calls this directly

Calls 2

zoomToMethod · 0.95
whenReadyMethod · 0.45

Tested by

no test coverage detected