打开图层。如何刷新集群?
OpenLayers. How to refresh cluster?
我动态地向我的集群添加功能,但是,据我所知,集群不起作用。我的图层定义如下:
var source = new ol.source.Vector({});
var cluster = new ol.source.Cluster({
distance: 10,
source: source
});
var style = new ol.style.Style({
fill: new ol.style.Fill({
color: "rgba(255,150,0,1)"
}),
stroke: new ol.style.Stroke({
color: "rgba(255,150,0,1)",
width: 1
}),
image: new ol.style.Circle({
radius: 1,
fill: new ol.style.Fill({
color: "rgba(255,150,0,1)"
})
}),
zIndex: 1
});
var layer = new ol.layer.Vector({
source: cluster,
style: style,
zIndex: 1
});
并且我在我的一个函数中批量添加了多个特征(点几何),该函数将图层作为参数。它是这样的:
layer.getSource().clear();
layer.setVisible(true);
layer.getSource().addFeatures(features); // features is a large array of features
但是,如果我放大和缩小,我会看到这些图片:
和:
在第二个屏幕截图中,您可以看到,我的层显示了所有特征并忽略了聚类参数。为什么会这样,我该如何解决? (PS。如果重要的话,我使用的是最新版本的 OL)
这工作正常,我用你的代码更新了这个例子并添加了具有随机值的特征
openlayers.org/en/latest/examples/cluster.html
<!DOCTYPE html>
<html>
<head>
<title>Clustered Features</title>
<link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css">
<!-- 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=requestAnimationFrame,Element.prototype.classList,URL"></script>
<script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script>
</head>
<body>
<div id="map" class="map"></div>
<form>
<label>cluster distance</label>
<input id="distance" type="range" min="0" max="100" step="1" value="40"/>
</form>
<script>
var distance = document.getElementById('distance');
var source = new ol.source.Vector({
});
var cluster = new ol.source.Cluster({
distance: 10,
source: source
});
var style = new ol.style.Style({
fill: new ol.style.Fill({
color: "rgba(255,150,0,1)"
}),
stroke: new ol.style.Stroke({
color: "rgba(255,150,0,1)",
width: 1
}),
image: new ol.style.Circle({
radius: 1,
fill: new ol.style.Fill({
color: "rgba(255,150,0,1)"
})
}),
zIndex: 1
});
var clusters = new ol.layer.Vector({
source: cluster,
style: style,
zIndex: 1
});
var map = new ol.Map({
layers: [clusters],
target: 'map',
view: new ol.View({
center: [0, 0],
zoom: 2
})
});
var count = 20000;
var features = new Array(count);
var e = 4500000;
for (var i = 0; i < count; ++i) {
var coordinates = [2 * e * Math.random() - e, 2 * e * Math.random() - e];
/*features[i] =*/ source.addFeature(new ol.Feature(new ol.geom.Point(coordinates)));
}
distance.addEventListener('input', function() {
cluster.setDistance(parseInt(distance.value, 10));
});
</script>
</body>
</html>
祝你好运
我动态地向我的集群添加功能,但是,据我所知,集群不起作用。我的图层定义如下:
var source = new ol.source.Vector({});
var cluster = new ol.source.Cluster({
distance: 10,
source: source
});
var style = new ol.style.Style({
fill: new ol.style.Fill({
color: "rgba(255,150,0,1)"
}),
stroke: new ol.style.Stroke({
color: "rgba(255,150,0,1)",
width: 1
}),
image: new ol.style.Circle({
radius: 1,
fill: new ol.style.Fill({
color: "rgba(255,150,0,1)"
})
}),
zIndex: 1
});
var layer = new ol.layer.Vector({
source: cluster,
style: style,
zIndex: 1
});
并且我在我的一个函数中批量添加了多个特征(点几何),该函数将图层作为参数。它是这样的:
layer.getSource().clear();
layer.setVisible(true);
layer.getSource().addFeatures(features); // features is a large array of features
但是,如果我放大和缩小,我会看到这些图片:
和:
在第二个屏幕截图中,您可以看到,我的层显示了所有特征并忽略了聚类参数。为什么会这样,我该如何解决? (PS。如果重要的话,我使用的是最新版本的 OL)
这工作正常,我用你的代码更新了这个例子并添加了具有随机值的特征 openlayers.org/en/latest/examples/cluster.html
<!DOCTYPE html>
<html>
<head>
<title>Clustered Features</title>
<link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css">
<!-- 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=requestAnimationFrame,Element.prototype.classList,URL"></script>
<script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script>
</head>
<body>
<div id="map" class="map"></div>
<form>
<label>cluster distance</label>
<input id="distance" type="range" min="0" max="100" step="1" value="40"/>
</form>
<script>
var distance = document.getElementById('distance');
var source = new ol.source.Vector({
});
var cluster = new ol.source.Cluster({
distance: 10,
source: source
});
var style = new ol.style.Style({
fill: new ol.style.Fill({
color: "rgba(255,150,0,1)"
}),
stroke: new ol.style.Stroke({
color: "rgba(255,150,0,1)",
width: 1
}),
image: new ol.style.Circle({
radius: 1,
fill: new ol.style.Fill({
color: "rgba(255,150,0,1)"
})
}),
zIndex: 1
});
var clusters = new ol.layer.Vector({
source: cluster,
style: style,
zIndex: 1
});
var map = new ol.Map({
layers: [clusters],
target: 'map',
view: new ol.View({
center: [0, 0],
zoom: 2
})
});
var count = 20000;
var features = new Array(count);
var e = 4500000;
for (var i = 0; i < count; ++i) {
var coordinates = [2 * e * Math.random() - e, 2 * e * Math.random() - e];
/*features[i] =*/ source.addFeature(new ol.Feature(new ol.geom.Point(coordinates)));
}
distance.addEventListener('input', function() {
cluster.setDistance(parseInt(distance.value, 10));
});
</script>
</body>
</html>
祝你好运