名前と写真、そしてもちろん経度と緯度を含む、さまざまな場所の配列があります。これらをそのまま地図に載せるとごちゃごちゃしてしまいます。だから私はClustersを使用しようとします。
機能の作成から始めます。
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))
});
}
次に、特徴を含むベクトルをクラスターに入力します。
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)
後で、フィーチャーから「名前」を取得する必要がある onclick メソッドがありますが、印刷できるのはジオメトリだけです。オブジェクトの名前がクラスターから消えるようなものです。たとえば、 a を実行すると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 メソッドは機能の名前を取得できません。非常に奇妙な「未定義」を返します。ヘルプ?何か助けてください。クラスターに追加されると、機能が存在しなくなるようなものです。