나는 이름과 그림, 그리고 물론 경도와 위도와 함께 많은 다른 위치의 배열을 가지고있다. 이것을지도에 직접 붙여 놓으면 혼란스러워집니다. 그래서 Clusters을 사용하려고합니다. features을 만드는OpenLayers의 클러스터에서 기능 가져 오기?
시작 :
var vSource = new ol.source.Vector({ features: features});
var vFeatures = vSource.getFeatures();
var clusterSource = new ol.source.Cluster({
distance: 20,
source: vSource});
내가 다음 몇 가지 아이콘
var clusters = new ol.layer.Vector({
source: clusterSource,
style : new ol.style.Style({
image: new ol.style.Icon(({
src: 'image/image.png'})),
text: new ol.style.Text({
font: '18px Helvetica, Arial Bold, sans-serif',
text: size.toString(),
fill: new ol.style.Fill({
color: '#fff'
})
})
map.add(clusters)
I와 클러스터의 스타일 :
for(var i = 0; i < points.length; i++){
features[i] = new ol.Feature({
population: 4000,
name : points[i].name,
geometry: new ol.geom.Point(
ol.proj.transform([
points[i].long,
points[i].lat],
'EPSG:4326', oProjection))
});
}
그때 기능을 가진 벡터 클러스터를 채울 나중에 기능에서 "이름"을 가져와야하는 onclick 메소드가 있습니다. 그러나 그것이 인쇄 할 수있는 유일한 것은 기하학입니다, 그것은 개체의 이름이 클러스터에서 사라지는 것과 같습니다. 예를 들어 clusterSource.getFeatures()
을 수행하면 []
인 빈 벡터가 반환됩니다.
function addOverlays(points){
for(var i = 0; i<points.length;i++){
var element = document.getElementById(points[i].id);
var popup = new ol.Overlay({
element: element,
positioning: 'bottom-center',
stopEvent: false
});
map.addOverlay(popup);
// display popup on click
}
// display popup on click
map.on('click', function(evt) {
var feature = map.forEachFeatureAtPixel(evt.pixel,
function(feature, layer) {
console.log("feature on click: ",feature);
return feature;
});
if (feature) {
var geometry = feature.getGeometry();
var coord = geometry.getCoordinates();
popup.setPosition(coord);
console.log(feature.get('name'));
$(element).popover({
'placement': 'bottom',
'html': true,
'content': feature.get('name') //THIS IS THE TROUBLE
});
$(element).popover('show');
} else {
$(element).popover('destroy');
}
});
}
addOverlay 메서드는 기능의 이름을 가져올 수 없으며 매우 이상한 "undefined"를 반환합니다. 도움? 어떤 도움을 주시겠습니까? 이는 클러스터에 추가 될 때 기능이 중지 된 것과 같습니다.
아주 좋은 해결책이지만, 아직 클러스터되지 않은 단수 기능에 대해서는 문제가 있지만 결국에는 얻을 것입니다. – radhoo
@radhoo 나는 이것이 오래되었다는 것을 알고 있지만 같은 문제를 발견하고'feature.get ('name')'을'feature.get ('features')로 대체하여 해결했다. get ('name')'in 그밖에. 도움이 되었기를 바랍니다. –
Tim의 코드는 클러스터뿐만 아니라 단일 기능을 모두 지원하도록 이미 수정되었습니다. 의견은 올 바르고 게시 된 코드와 일치합니다. – radhoo