I've been trying to use the basic example from the OpenLayers website to create a simple clustering map for my GeoJSON data. However, I'm not getting any luck generating any clusters.
Below is my code:
var baseLayer = new ol.layer.Tile({
source: source: new ol.source.OSM()
});
var view = new ol.View ({
center: [19425838, -4409677], // projection is in EPSG: 3857
zoom: 6,
maxZoom: 28,
minZoom: 1
});
var source = new ol.source.GeoJSON({object: geojson_jobs}); // local .js file passing geoJSON features into a variable
var clustersSource = new ol.source.Cluster({
distance: 10,
source: source
});
var styleCache = {};
var clusters = new ol.layer.Vector({
source: clustersSource,
style: function(feature, resolution) {
var size = feature.get('features').length;
var style = styleCache[size];
if (true) {
style = [
new ol.style.Style({
image: new ol.style.Circle({
radius: resolution,
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 layers = [baseLayer, clusters];
var map = new ol.Map({
controls: ol.control.defaults().extend([new ol.control.ScaleLine({})]),
target: 'map',
renderer: 'canvas',
view: view,
layers: layers
});
Best Answer
It doesn't work with geoJSON like that, it took me a day to figure this out, you need to make sure that:
If you load geoJSON from a file in contrast to the example where it gets generated in an array you have to do this trick below.
The example code
The geoJSON from file
The rest
ol3 clustering is a bit of a pain..
Check out these links: