OpenLayers 4 ol.source 详解
source 是 Layer 的重要組成部分,表示圖層的來源,也就是服務地址。
除了在構造函數中指定外,還可以使用 layer.setSource(source) 稍后指定。
一、包含的類型
上面介紹的都是可以實例化的類,還有一部分基類,不能被實例化,只負責被繼承,有:
- ol.source.Image,提供單一圖片數據的類型,直接繼承自 ol.source.Source;
- ol.source.Tile,提供被切分為網格切片的圖片數據,繼承自 ol.source.Source;
- ol.source.Vector,提供矢量圖層數據,繼承自 ol.source.Source;
二、用法說明
1. ol.source.Vector
矢量圖層的數據源
1.1 事件
包含四個事件,addfeature,changefeature,clear,removefeature。
addfeature,當一個要素添加到 source 中觸發。
changefeature,當要素變化時觸發。
clear,當 source 的 clear 方法調用時候觸發。
removefeature,當要素移除時候觸發。
1.2 參數
接受的參數:
?
/*** @typedef {{attributions: (Array.<ol.Attribution>|undefined),* features: (Array.<ol.Feature>|undefined),* format: (ol.format.Feature|undefined),* loader: (ol.FeatureLoader|undefined),* logo: (string|olx.LogoOptions|undefined),* strategy: (ol.LoadingStrategy|undefined),* url: (string|undefined),* wrapX: (boolean|undefined)}}* @api*/- attribution,地圖右下角的 logo 包含的內容;
- features,地理要素,從字符串讀取的數據;
- format,url屬性設置后,加載要素使用的數據格式,采用異步的 AJAX 加載;
- loader,加載要素使用的加載函數;
- logo,logo包含的內容;
- strategy,加載要素使用的策略,默認是 一次性加載所有要素;
- url,要素數據的地址;
- wrapX,是否在地圖水平坐標軸上重復,默認是 true。
1.3 實例
features 方法
假如有一個包含空間數據的字符串,geojsonObject,是GeoJSON字符串格式,那么可以用來初始化一個圖層。
?
var vectorSource = new ol.source.Vector({features: (new ol.format.GeoJSON()).readFeatures(geojsonObject) });var vectorLayer = new ol.layer.Vector({source: vectorSource,style: style });map.addLayer(vectorLayer);url + format 方法
如果有一個文件作為數據源,那么可以配置 url 屬性來加載數據:
?
var vectorLayer = new ol.layer.Vector({source: new ol.source.Vector({url: 'data/geojson/countries.geojson',format: new ol.format.GeoJSON()}) });這兩種方法中都會指定數據來源格式, 矢量數據源支持的格式包含很多:gml、EsriJSON、geojson、gpx、igc、kml、osmxml、ows、polyline、topojson、wfs、wkt、wms capabilities(兼容 wms 的格式)、 wms getfeatureinfo、 wmts capabilities、xlink、xsd等格式。這些格式都有readFeatures 、readFeature 和readGeometry 方法用于讀取數據。
2. ol.source.Tile
提供被切分為切片的圖片地圖數據
可配置的選項
?
/*** @typedef {{attributions: (Array.<ol.Attribution>|undefined),* extent: (ol.Extent|undefined),* logo: (string|olx.LogoOptions|undefined),* opaque: (boolean|undefined),* tilePixelRatio: (number|undefined),* projection: ol.proj.ProjectionLike,* state: (ol.source.State|undefined),* tileGrid: (ol.tilegrid.TileGrid|undefined),* wrapX: (boolean|undefined)}}*/與 vector 一樣的選項就不介紹了,介紹與 vector 特有的選項:
- extent,地圖視圖默認的坐標范圍;
- opaque,是否透明,一個布爾值,默認 false;
- tilePixelRatio,切片的大小調整選項,如 256 × 256,和 512 × 512;
- projection,投影坐標系;
- state,地圖所處的狀態,包括undefined,loading,ready,error;
- tileGrid,覆蓋在地圖上的格網;
接受的事件
- tileloadstart,切片開始加載時觸發的事件;
- tileloadend,切片加載完畢觸發事件;
- tileloaderror,切片加載出錯時的處理。
3. ol.source.Image
提供單一的圖片地圖。
可以配置的選項
?
/*** @typedef {{attributions: (Array.<ol.Attribution>|undefined),* extent: (null|ol.Extent|undefined),* logo: (string|olx.LogoOptions|undefined),* projection: ol.proj.ProjectionLike,* resolutions: (Array.<number>|undefined),* state: (ol.source.State|undefined)}}*/- resolutions,地圖分辨率。其他的選項都與以上的一樣。
觸發的事件
- imageloadstart,圖片地圖開始加載觸發的事件;
- imageloadend,圖片地圖加載完畢觸發的事件;
- imageloaderror,圖片地圖加載出錯時觸發的事件。
四、總結
source 是 layer 中必須(required)的選項,定義了地圖數據的來源,與數據有關的函數,如addfeature、getfeature等函數都定義在 source 中,而且數據源支持多種格式。
OpenLayers 3 之 地圖圖層數據來源(ol.source)詳解
ol.source API
?
總結
以上是生活随笔為你收集整理的OpenLayers 4 ol.source 详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java三大版本,JDK,JER,JVM
- 下一篇: 电容的区别,关键在于介质