nakarte

Source code of https://map.sikmir.ru (fork)
git clone git://git.sikmir.ru/nakarte
Log | Files | Refs | LICENSE

index.js (2273B)


      1 import L from 'leaflet';
      2 
      3 class LayerCutlineOverview extends L.Layer {
      4     options = {
      5         style: {
      6             stroke: true,
      7             color: '#db5a00',
      8             weight: 4,
      9             opacity: 1,
     10             fill: true,
     11             fillOpacity: 0.2,
     12         },
     13     };
     14 
     15     constructor(cutline, maxOverviewZoom, text) {
     16         super();
     17         this.cutline = cutline;
     18         this.maxZoom = maxOverviewZoom;
     19         this.text = text;
     20         this._features = L.featureGroup();
     21         this._isCutlineRequested = false;
     22     }
     23 
     24     onAdd() {
     25         if (!this._isCutlineRequested) {
     26             this._isCutlineRequested = true;
     27             this._createFeatures().then(this._updateVisibility.bind(this));
     28         }
     29         this._updateVisibility();
     30         this._map.on('zoomend', this._updateVisibility, this);
     31     }
     32 
     33     onRemove() {
     34         this._map.off('zoomend', this._updateVisibility, this);
     35         this._map.removeLayer(this._features);
     36     }
     37 
     38     async _createFeatures() {
     39         let cutlinePromise = this.cutline;
     40         if (typeof cutlinePromise === 'function') {
     41             cutlinePromise = cutlinePromise();
     42         }
     43         if (!cutlinePromise.then) {
     44             cutlinePromise = Promise.resolve(cutlinePromise);
     45         }
     46         let cutlineCoords;
     47         try {
     48             cutlineCoords = await cutlinePromise;
     49         } catch (_) {
     50             // will be handled as empty later
     51         }
     52         if (cutlineCoords) {
     53             for (const lineString of cutlineCoords) {
     54                 const feature = L.polygon(
     55                     lineString.map(([lon, lat]) => [lat, lon]),
     56                     this.options.style
     57                 );
     58                 feature.bindTooltip(this.text, {sticky: true});
     59                 feature.on('click', (e) => this._map.setView(e.latlng, this.maxZoom + 1));
     60                 this._features.addLayer(feature);
     61             }
     62         }
     63     }
     64 
     65     _updateVisibility() {
     66         if (!this._map) {
     67             return;
     68         }
     69         const zoom = this._map.getZoom();
     70         if (zoom <= this.maxZoom) {
     71             this._map.addLayer(this._features);
     72             this._features.bringToBack();
     73         } else {
     74             this._map.removeLayer(this._features);
     75         }
     76     }
     77 }
     78 
     79 export {LayerCutlineOverview};