类:聚类

ol/source/Cluster~聚类


import Cluster from 'ol/source/Cluster.js';

用于聚类矢量数据的图层源。开箱即用地支持点几何要素。对于其他几何类型,或若非所有几何要素均需参与聚类,可定义自定义的 geometryFunction

如果实例被释放但未同时释放底层数据源,则必须调用 setSource(null) 以从包装的数据源中移除监听器引用。

new Cluster(options)

Name Type 描述
attributions AttributionLike | undefined

版权信息。

distance number (defaults to 20)

要素聚集的像素距离。

minDistance number (defaults to 0)

聚类之间的最小像素距离。该距离将被限制在配置的范围内。默认不保证最小距离。此配置可用于避免图标重叠。作为权衡,聚类要素的位置将不再位于其所有要素的中心。

geometryFunction GeometryFunction<FeatureType> | undefined

接收一个参数的函数Feature作为参数并返回一个Point作为要素的聚类计算点。当某个要素不应被纳入聚类时,该函数应返回null默认值在底层数据源仅包含点要素时有效。

function(feature) {
  return feature.getGeometry();
}

参见getInteriorPoint获取多边形聚类计算点的方法

createCluster function | undefined

接受聚类中心的函数Point以及一个数组Feature包含于该聚类中。必须返回一个Feature将用于渲染。默认实现为:

function(point, features) {
  return new Feature({
    geometry: point,
    features: features
  });
}
source VectorSource<FeatureType> (defaults to null)

数据源.

wrapX boolean (defaults to true)

是否水平循环世界。

触发事件:

继承

方法

addFeature(feature) inherited

向源中添加单个要素。若需一次性添加一批要素,请改为调用 #addFeatures()。如果具有相同 ID 的要素已存在,则该要素不会被添加到源中。此举旨在避免在使用边界框或图块加载策略时出现要素重复。注意:若 Collection 用于要素,此规则同样适用,即如果在集合中添加了具有重复 ID 的要素,它将立即从集合中移除。

Name Type 描述
feature FeatureType

要添加的要素。

addFeatures(features) inherited

向数据源添加一批要素。

Name Type 描述
features Array.<FeatureType>

要添加的要素。

递增修订计数器并触发变更事件。

从数据源中移除所有要素。

Name Type 描述
fast boolean | undefined

跳过事件分发removefeature事件.

dispatchEvent(event){boolean | undefined} inherited

触发一个事件,并调用所有侦听此类型事件的侦听器。事件参数可以是字符串,也可以是具有 type 属性的对象。

Name Type 描述
event BaseEvent | string

事件对象。

返回:
false 如果事件对象上被调用了 preventDefault 方法,或者任何监听器返回了 false。

forEachFeature(callback){T | undefined} inherited

遍历数据源中的所有要素,对每个要素调用提供的回调函数。如果回调返回任何真值,迭代将停止,并且函数将返回该值。注意:此函数仅遍历几何已定义的要素。

Name Type 描述
callback function

对源中的每个要素进行调用。返回真值以停止迭代。

返回:
回调函数最后一次调用的返回值。

forEachFeatureInExtent(extent, callback){T | undefined} inherited

遍历所有边界框与指定范围相交的要素(请注意,要素的几何图形可能与范围不相交),并为每个要素调用回调函数。如果回调函数返回真值,则迭代将停止,且函数将返回该值。

若您关注几何与范围相交的要素,请改用 #forEachFeatureIntersectingExtent() 方法。

useSpatialIndex 设置为 false 时,该方法将遍历所有要素,等效于 #forEachFeature()

Name Type 描述
extent Extent

范围.

callback function

对每个边界框与指定范围相交的要素进行调用。

返回:
最后一次回调调用的返回值。

forEachFeatureIntersectingExtent(extent, callback){T | undefined} inherited

遍历所有几何与指定范围相交的要素,并对每个要素调用回调函数。若回调函数返回真值,则迭代终止,函数返回该真值。

若仅需测试边界框相交,请调用 #forEachFeatureInExtent() 方法。

Name Type 描述
extent Extent

范围.

callback function

对几何与提供范围相交的每个要素进行调用。

返回:
回调函数最后一次调用的返回值。

获取值。

Name Type 描述
key string

键名。

返回:
值。

获取数据源的归属函数

返回:
归属功能。

getAttributionsCollapsible(){boolean} inherited

返回:
版权信息可折叠。

getClosestFeatureToCoordinate(coordinate, filter){FeatureType | null} inherited

获取距离给定坐标最近的要素。

当数据源中 useSpatialIndex 设置为 false,且要素类型为 Feature 时,此方法不可用。

Name Type 描述
coordinate Coordinate

坐标

filter function | undefined

要素过滤函数。 过滤函数将接收一个参数,即feature应返回布尔值。默认不进行过滤。

返回:
最近的要素(若未找到,则返回 null)。

getDistance(){number}

计算聚类之间的像素距离。

返回:
距离.

获取当前源中要素的范围。

当数据源将 useSpatialIndex 设置为 false 时,此方法不可用。

Name Type 描述
extent Extent | undefined

目标范围。如果已指定,则不会创建新范围,而是直接覆盖其坐标。

返回:
范围.

getFeatureById(id){FeatureClassOrArrayOfRenderFeatures<FeatureType> | null} inherited

通过其标识符(即 feature.getId() 返回的值)获取要素。当使用 RenderFeatures 时,getFeatureById() 可以返回一个 RenderFeatures 数组。这允许处理 GeometryCollection 几何图形,其中格式读取器为每个 GeometryCollection 成员创建一个 RenderFeature。请注意,索引将字符串和数字标识符视为相同。因此 source.getFeatureById(2) 将返回 id 为 '2'2 的要素。

Name Type 描述
id string | number

要素标识符。

返回:
要素(若未找到,则为 null)。

getFeatures(){Array.<FeatureType>} inherited

以随机顺序获取数据源上当前要素的快照。返回的数组是一个副本,要素是对数据源中要素的引用。

返回:
要素。

getFeaturesAtCoordinate(coordinate){Array.<FeatureType>} inherited

获取几何图形与给定坐标相交的所有要素。

Name Type 描述
coordinate Coordinate

坐标。

返回:
要素。

getFeaturesCollection(){Collection<FeatureType> | null} inherited

获取与此数据源关联的要素集合。将为 null,除非数据源在配置时设置了 useSpatialIndexfalse,或者配置了 Collectionfeatures

返回:
要素集

getFeaturesInExtent(extent, projection){Array.<FeatureType>} inherited

获取边界框与提供的范围相交的所有要素。请注意,此方法会以随机顺序返回与给定范围相交的所有要素的数组(因此可能包含几何不与范围相交的要素)。

useSpatialIndex 设置为 false 时,该方法将返回所有要素。

Name Type 描述
extent Extent

范围.

projection Projection | undefined

包含要素,其中extent超出x轴范围projection并环绕世界。

返回:
要素。

getFormat(){FeatureFormat<FeatureType> | null} inherited

获取该数据源的格式。

返回:
} 要素格式。

getKeys(){Array.<string>} inherited

获取对象属性名称列表

返回:
属性列表

getMinDistance(){number}

配置的聚类间最小距离。

返回:
最小像素距离。

getProjection(){Projection | null} inherited

获取源的投影。

返回:
投影.

getProperties(){Object.<string, *>} inherited

获取所有属性名及其对应值的对象。

返回:
对象。

getRevision(){number} inherited

获取该对象的版本号。每次修改对象时,其版本号都会递增。

返回:
修订。

getSource(){VectorSource<FeatureType> | null}

获取包装源的引用。

返回:
数据源.

获取源的状态,请参阅 State 了解可能的状态。

返回:
状态。

getUrl(){string | FeatureUrlFunction | undefined} inherited

获取与此数据源关联的 URL。

返回:
网址。

hasFeature(feature){boolean} inherited

若要素被包含在源中,则返回 True。

Name Type 描述
feature FeatureType

要素.

返回:
有要素。

on(type, listener){EventsKey | Array<EventsKey>} inherited

监听特定类型的事件。

Name Type 描述
type string | Array.<string>

事件类型或事件类型数组。

listener function

监听器函数。

返回:
监听器的唯一键。如果以事件类型数组作为第一个参数调用,则返回将是键的数组。

once(type, listener){EventsKey | Array<EventsKey>} inherited

仅监听一次特定类型的事件。

Name Type 描述
type string | Array.<string>

事件类型或事件类型数组。

listener function

监听器函数。

返回:
监听器的唯一键。如果以事件类型数组作为第一个参数调用,则返回将是键的数组。

removeFeature(feature) inherited

从源中移除单个要素。若要批量移除要素,请改用 #removeFeatures() 方法。

Name Type 描述
feature FeatureType

要删除的要素。

removeFeatures(features) inherited

从数据源中批量删除要素。若要一次性删除所有要素,请改用 #clear() 方法。

Name Type 描述
features Array.<FeatureType>

要删除的要素。

removeLoadedExtent(extent) inherited

从已加载范围列表中移除一个范围。

Name Type 描述
extent Extent

范围.

set(key, value, silent) inherited

设置值。

Name Type 描述
key string

键名。

value *

值。

silent boolean | undefined

更新而不触发事件。

setAttributions(attributions) inherited

设置数据源的归属信息。

Name Type 描述
attributions AttributionLike | undefined

归属。可以传递为string, Array<string>, Attribution,或者undefined.

setDistance(distance)

设置要素聚类的距离。

Name Type 描述
distance number

像素距离

setLoader(loader) inherited

设置数据源的新加载器。下一个渲染周期将使用新的加载器。

Name Type 描述
loader FeatureLoader

要设置的加载器。

setMinDistance(minDistance)

设置簇之间的最小距离,上限为配置的距离。

Name Type 描述
minDistance number

最小像素距离。

setProperties(values, silent) inherited

设置一组键值对。请注意,此操作将修改现有属性并添加新属性,但不会删除任何现有属性。

Name Type 描述
values Object.<string, *>

值。

silent boolean | undefined

更新而不触发事件。

setSource(source)

替换已封装的源。

Name Type 描述
source VectorSource<FeatureType> | null

该实例的新源

将源指向新的URL。下一个渲染周期将使用新的URL。

Name Type 描述
url string | FeatureUrlFunction

网址。

un(type, listener) inherited

取消监听特定类型的事件。

Name Type 描述
type string | Array.<string>

事件类型或事件类型数组。

listener function

监听函数。

unset(key, silent) inherited

清除属性。

Name Type 描述
key string

键名。

silent boolean | undefined

在不触发事件的情况下取消设置。