图层相关

2020-06-09  本文已影响0人  扶不起的蝌蚪

除特殊注明的HTML结构
默认均为

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Image ArcGIS MapServer</title>
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=fetch,requestAnimationFrame,Element.prototype.classList,URL"></script>
    <style>
      .map {
        width: 100%;
        height:400px;
      }
    </style>
  </head>
  <body>
    <div id="map" class="map"></div>
    <script src="index.js"></script>
  </body>
</html>

加载ArcGIS图片图层


var layers =  new ol.layer.Image({
    source: new ImageArcGISRest({
      ratio: 1,
      params: {},
      url: `https://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer`
    })
  })
var map = new Map({
  layers: layer,
  target: 'map',
  view: new View({
    center: [-10997148, 4569099],
    zoom: 4
  })
});

[加载ArcGIS瓦片图层]

这种source类型支持地图和图片服务,为了缓存ArcGis服务,但是最好的方式是推荐使用o.source.XYZ

import 'ol/ol.css';
import Map from 'ol/Map';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
import {OSM, TileArcGISRest} from 'ol/source';

var url = 'https://sampleserver1.arcgisonline.com/ArcGIS/rest/services/' +
    'Specialty/ESRI_StateCityHighway_USA/MapServer';

var layers = [
  new TileLayer({
    source: new OSM()
  }),
  new TileLayer({
    extent: [-13884991, 2870341, -7455066, 6338219],
    source: new TileArcGISRest({
      url: url
    })
  })
];
var map = new Map({
  layers: layers,
  target: 'map',
  view: new View({
    center: [-10997148, 4569099],
    zoom: 4
  })
});

图层的显示与隐藏

通过setVisible(Boolean)来控制图层的显示与隐藏

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Bing Maps</title>
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=fetch,requestAnimationFrame,Element.prototype.classList,URL"></script>
    <style>
      .map {
        width: 100%;
        height:400px;
      }
    </style>
  </head>
  <body>
     <div id="map" class="map"></div>
     <select id="layer-select">
       <option value="Aerial">Aerial</option>
       <option value="AerialWithLabelsOnDemand" selected>Aerial with labels</option>
       <option value="RoadOnDemand">Road</option>
       <option value="CanvasDark">Road dark</option>
       <option value="OrdnanceSurvey">Ordnance Survey</option>
     </select>
    <script src="index.js"></script>
  </body>
</html>
var styles = [
  'RoadOnDemand',
  'Aerial',
  'AerialWithLabelsOnDemand',
  'CanvasDark',
  'OrdnanceSurvey'
];
var layers = [];
for (var i = 0 ; i < styles.length; ++i) {
  layers.push(new TileLayer({
    visible: false,
    preload: Infinity,
    source: new BingMaps({
      key: 'Your Bing Maps Key from http://www.bingmapsportal.com/ here ',
      imagerySet: styles[i]
      // 超过19级的放大后,BING图层会显示为一张标识没有图层的图标,如果为了不显示此效果,你需要最大的放大层级设置为19
      // maxZoom: 19
    })
  }));
}
var map = new Map({
  layers: layers,
  target: 'map',
  view: new View({
    center: [-6655.5402445057125, 6709968.258934638],
    zoom: 13
  })
});

var select = document.getElementById('layer-select');
function onChange() {
//通过遍历匹配与当前选中值相等的图层,使其显示
  for (var i = 0 ; i < layers.length; ++i) {
    layers[i].setVisible(styles[i] === select.value;);
  }
}
select.addEventListener('change', onChange);
//初始化选中的图层
onChange();
上一篇下一篇

猜你喜欢

热点阅读