Module: ol/source/Vector

ol/source/Vector


Classes

VectorSourceEvent
VectorSource

Type Definitions

LoadingStrategy()

接受 Extent 和分辨率作为参数,并返回要加载的范围数组的函数。通常是标准 ol/loadingstrategy 策略之一。

Options{Object}

Properties:
Name Type Description
attributions AttributionLike | undefined

归属信息。

features Array.<要素Type> | Collection<要素Type> | undefined

要素集合。如果作为 Collection 提供,数据源中的要素和集合将保持同步。

format 要素Format<要素Type> | undefined

设置 url 时 XHR 要素加载器使用的要素格式。如果设置了 url 则必需,否则忽略。

loader 要素Loader<要素Type> | undefined

用于加载要素的加载函数,例如从远程数据源。如果未设置此项且设置了 url,数据源将创建并使用 XHR 要素加载器。'featuresloadend''featuresloaderror' 事件仅在使用 successfailure 回调时触发。

示例:

import Vector from 'ol/source/Vector.js';
import GeoJSON from 'ol/format/GeoJSON.js';
import {bbox} from 'ol/loadingstrategy.js';

const vectorSource = new Vector({
  loader: async (extent, resolution, projection) => {
     const url = 'https://ahocevar.com/geoserver/wfs?service=WFS&' +
         'version=1.1.0&request=Get要素&typename=osm:water_areas&' +
         'outputFormat=application/json' +
         'bbox=' + extent.join(',') + ',' + projection.getCode();
     const response = await fetch(url);
     if (!response.ok) {
       throw new Error('Network response was not ok');
     }
     const json = await response.json();
     const features = new GeoJSON().read要素s(json, {
       featureProjection: projection,
     });
     return features;
   },
   strategy: bbox,
 });

当您想要重试失败的请求时,请使用

vector数据源。removeLoadedExtent(extent);
vector数据源。changed();
overlaps boolean
(defaults to true)

此数据源可能有重叠的几何图形。将此设置为 false(例如对于表示行政边界多边形的数据源或 TopoJSON 数据源)允许渲染器优化填充和描边操作。

strategy LoadingStrategy | undefined

要使用的加载策略。默认使用 all 策略,即一次性加载所有要素的策略。

url string | featureUrlFunction | undefined

设置此选项指示数据源使用 XHR 加载器加载要素(参见 xhr)。使用 stringall 可从给定 URL 一次性下载所有要素。使用 featureUrlFunction 可通过其他加载策略生成 URL。需要同时设置 format。当提供默认 XHR 要素加载器时,要素将在解析期间从数据投影转换到视图投影。如果远程数据源未正确声明其投影,此转换将不正确。对于某些格式,可以通过在格式上设置 dataProjection 构造函数选项来覆盖默认投影(通常为 EPSG:4326)。注意如果数据源包含非要素数据(如 GeoJSON 几何图形或 KML NetworkLink),这些将被忽略。请使用自定义加载器加载这些数据。

useSpatialIndex boolean
(defaults to true)

默认情况下,使用 RTree 作为空间索引。当频繁添加和移除要素且要素总数较少时,将此设置为 false 可能会提高性能。

注意当 useSpatialIndex 设置为 false 时,无法使用 getFeaturesInExtentgetClosestFeatureToCoordinategetExtent,且 forEachFeatureInExtent 将遍历所有要素。

设置为 false 时,要素将保存在 Collection 中,可通过 getFeaturesCollection 获取。

wrapX boolean
(defaults to true)

水平包裹世界。要使跨越 -180° 和 180° 子午线的矢量编辑正常工作,应设置为 false。生成的几何图形坐标将超出世界边界。