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