I have the code to display 7 points with blue color. Now I need to display 7 points in different colors (for example 2 points with red color, 2 points with green color and 3 points with blue color). And I need points clustering work for these points as well.
My code: https://codepen.io/quas1k/pen/WgWRNE
var coordinates = ol.proj.fromLonLat([30.5238, 50.45466]);
var coordinates1 = ol.proj.fromLonLat([30.5238, 50.45166]);
var coordinates2 = ol.proj.fromLonLat([30.5538, 50.45466]);
var coordinates3 = ol.proj.fromLonLat([30.5518, 50.45466]);
var coordinates4 = ol.proj.fromLonLat([30.5558, 50.45466]);
var coordinates5 = ol.proj.fromLonLat([30.5598, 50.45466]);
var coordinates6 = ol.proj.fromLonLat([30.5510, 50.45466]);
var coordinates7 = ol.proj.fromLonLat([30.5580, 50.45466]);
var features = [];
features[0] = new ol.Feature(new ol.geom.Point(coordinates));
features[1] = new ol.Feature(new ol.geom.Point(coordinates1));
features[2] = new ol.Feature(new ol.geom.Point(coordinates2));
features[3] = new ol.Feature(new ol.geom.Point(coordinates3));
features[4] = new ol.Feature(new ol.geom.Point(coordinates4));
features[5] = new ol.Feature(new ol.geom.Point(coordinates5));
features[6] = new ol.Feature(new ol.geom.Point(coordinates6));
features[7] = new ol.Feature(new ol.geom.Point(coordinates7));
var source = new ol.source.Vector({
features: features
});
var clusterSource = new ol.source.Cluster({
distance: 40,
source: source
});
var styleCache = {};
var clusters = new ol.layer.Vector({
source: clusterSource,
style: function(feature) {
var size = feature.get('features').length;
var style = styleCache[size];
if (!style) {
style = new ol.style.Style({
image: new ol.style.Circle({
radius: 12,
stroke: new ol.style.Stroke({
color: '#fff'
}),
fill: new ol.style.Fill({
color: '#3399CC'
})
}),
text: new ol.style.Text({
text: size.toString(),
fill: new ol.style.Fill({
color: '#fff'
})
})
});
styleCache[size] = style;
}
return style;
}
});
var raster = new ol.layer.Tile({
source: new ol.source.OSM()
});
var map = new ol.Map({
layers: [raster, clusters],
target: 'map',
view: new ol.View({
center: ol.proj.fromLonLat([30.5238, 50.45466]),
zoom: 11
})
});
How I can change this code to achieve the described behaviour? I have problems with both displaying the points in different colors and further clustering them by color.
Use something like this:
features[0] = new ol.Feature({geometry: new ol.geom.Point(coordinates), color: 'red'});
features[1] = new ol.Feature({geometry: new ol.geom.Point(coordinates1), color: 'red'});
features[2] = new ol.Feature({geometry: new ol.geom.Point(coordinates2), color: 'green'});
features[3] = new ol.Feature({geometry: new ol.geom.Point(coordinates3), color: 'green'});
features[4] = new ol.Feature({geometry: new ol.geom.Point(coordinates4), color: 'green'});
features[5] = new ol.Feature({geometry: new ol.geom.Point(coordinates5), color: 'blue'});
features[6] = new ol.Feature({geometry: new ol.geom.Point(coordinates6), color: 'blue'});
features[7] = new ol.Feature({geometry: new ol.geom.Point(coordinates7), color: 'blue'});
Clusters will take the color of the first point in the cluster. I've removed styleCache as the results will keep changing
var size = feature.get('features').length;
var color = feature.get('features')[0].get('color');
var style = new ol.style.Style({
image: new ol.style.Circle({
radius: 12,
stroke: new ol.style.Stroke({
color: '#fff'
}),
fill: new ol.style.Fill({
color: color
})
}),
text: new ol.style.Text({
text: size.toString(),
fill: new ol.style.Fill({
color: '#fff'
})
})
});