簡體   English   中英

Openlayers - 僅顯示高於特定縮放級別的圖層

[英]Openlayers - Only display layers above a certain zoom level

我正在處理具有不同矢量圖層的開放圖層地圖。 我想添加一個功能,即某個矢量圖層僅以特定的縮放值顯示,例如在縮放 >= 18 時。我嘗試使用函數 minZoom 和 minScale / maxScale,但它不起作用。 我想以 >= 18 的縮放級別顯示的矢量圖層稱為“dach 層”。 我也嘗試用 if 函數解決它:

// dach layer anzeigen versuch
const dach = document.getElementById('dach');
dach.addEventListener('click', function (event) {
  var checkBox = document.getElementById("dach");
  if (checkBox.checked == true) {
    if (map.getZoom() > 17) {
      dachLayer.setMap(map);
      //hitzeLayer.setVisible(true);
    }
   } else {
      //hitzeLayer.setVisible(false);
      dachLayer.setMap(undefined);
    }
}); 

這是我使用的代碼:

import 'ol/ol.css';
import Map from 'ol/Map';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
import Stamen from 'ol/source/Stamen';
import VectorLayer from 'ol/layer/Vector';
import Vector from 'ol/source/Vector';
import GeoJSON from 'ol/format/GeoJSON';
import Style from 'ol/style/Style';
import Circle from 'ol/style/Circle';
import Fill from 'ol/style/Fill';
import Stroke from 'ol/style/Stroke';
import Overlay from 'ol/Overlay';
import {
  fromLonLat,
  toLonLat
} from 'ol/proj';
import sync from 'ol-hashed';
import OSM from 'ol/source/OSM';
import Feature from 'ol/Feature';
import {
  circular
} from 'ol/geom/Polygon';
import Point from 'ol/geom/Point';
import Control from 'ol/control/Control';
import * as olProj from 'ol/proj';
import XYZ from 'ol/source/XYZ';

// define the map
const map = new Map({
  target: 'map',
  view: new View({
    center: fromLonLat([16.37, 48.2]),
    zoom: 13
  })
});

sync(map);

//Adresssuche
const searchResultSource = new Vector();
const searchResultLayer = new VectorLayer({
  source: searchResultSource
});

searchResultLayer.setStyle(new Style({
  image: new Circle({
    fill: new Fill({
      color: 'rgba(0, 128, 0, 1)'
    }),
    stroke: new Stroke({
      color: '#000000',
      width: 1.25
    }),
    radius: 15
  })
}));

var element = document.getElementById('search');
element.addEventListener('keydown', listenerFunction);

function listenerFunction(event) {
  console.log(event);
  console.log(event.keyCode);
  if (event.keyCode === 13) {

    const xhr = new XMLHttpRequest;
    xhr.open('GET', 'https://photon.komoot.de/api/?q=' + element.value + '&limit=3');
    xhr.onload = function () {
      const json = JSON.parse(xhr.responseText);
      const geoJsonReader = new GeoJSON({
        featureProjection: 'EPSG:3857'
      });
      searchResultSource.clear();
      const features = geoJsonReader.readFeatures(json);
      console.log(features);
      searchResultSource.addFeatures(features);
      if (!searchResultSource.isEmpty()) {
        map.getView().fit(searchResultSource.getExtent(), {
          maxZoom: 18,
          duration: 500
        });
      }
    };
    xhr.send();


  }
}

//OpenStreetMap
const OSMbaseLayer = new TileLayer({
  type: 'base',
  source: new OSM()
});

// Statellit
const satellitLayer = new TileLayer({
  source: new XYZ({
    attributions: ['Powered by Esri', 'Source: Esri, DigitalGlobe, GeoEye, Earthstar Geographics, CNES/Airbus DS, USDA, USGS, AeroGRID, IGN, and the GIS User Community'],
    attributionsCollapsible: false,
    url: 'https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
    maxZoom: 30
  })
});

//shape
const dachLayer = new VectorLayer({
  source: new Vector({
    name: 'dach',
    url: 'data/dach.geojson',
    format: new GeoJSON()
  })
});

dachLayer.setStyle(new Style({
  fill: new Fill({
    color: 'brown'
  }),
  stroke: new Stroke({
    color: 'brown',
    width: 0.25
  }),
}));

// Layer hinzufügen
map.addLayer(OSMbaseLayer);
map.addLayer(searchResultLayer);
dachLayer.setZIndex(15);

// dach layer anzeigen
const dach = document.getElementById('dach');
dach.addEventListener('click', function (event) {
  var checkBox = document.getElementById("dach");
  if (checkBox.checked == true) {
    dachLayer.setMap(map);
    //hitzeLayer.setVisible(true);
  } else {
    //hitzeLayer.setVisible(false);
    dachLayer.setMap(undefined);
  }
}); 

// Get the OSMbase Base-Button
const OSMbase = document.getElementById('OSMbase');
OSMbase.addEventListener('click', function (event) {
  //contr.style.color = 'ffffff';
  //Andere Layer entfernen
  map.removeLayer(satellitLayer);
  map.removeLayer(searchResultLayer);
  //OSM Layer hinzufügen
  map.addLayer(OSMbaseLayer);
  map.addLayer(searchResultLayer);
});

// Get the satellit Base-Button
const satellit = document.getElementById('satellit');
satellit.addEventListener('click', function (event) {
  //Andere Layer entfernen
  map.removeLayer(OSMbaseLayer);
  map.removeLayer(searchResultLayer);
  //Satelliten Layer hinzufügen
  map.addLayer(satellitLayer);
  map.addLayer(searchResultLayer);
});

getZoom()方法派生自 View 模塊:

https://openlayers.org/en/latest/apidoc/module-ol_View-View.html#getZoom

以下工作:

dach.addEventListener('click', function (event) {
  var checkBox = document.getElementById("dach");
  if (checkBox.checked == true) {
    if (map.getview().getZoom() > 17) {
      dachLayer.setMap(map);
      //hitzeLayer.setVisible(true);
    }
   } else {
      //hitzeLayer.setVisible(false);
      dachLayer.setMap(undefined);
    }
}); 

同時,您可能希望查看地圖上的 'moveend' 事件以偵聽縮放更改而不是單擊:

map.on('moveend', () => {
// do kewl things
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM