Last active
December 13, 2018 11:06
-
-
Save vedro-compota/645f0f2680ab36311194c5e8ed6d5afa to your computer and use it in GitHub Desktop.
osm openlayers cluster add feature proble example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="chrome=1"> | |
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width"> | |
<style> | |
html, body, .map { height: 100%; width: 100%; }; | |
</style> | |
<link href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" rel='stylesheet' type='text/css' /> | |
</head> | |
<body> | |
<div id="map" class="map"></div> | |
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js" type="text/javascript"></script> | |
<script src="index.js" type="text/javascript"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var source = new ol.source.Vector({}); | |
var clusterSource = new ol.source.Cluster({ | |
distance: 40, | |
source: source | |
}); | |
var styleCache = {}; | |
var clusters = new ol.layer.Vector({ | |
source: clusterSource, | |
style: function(feature, resolution) { | |
var size = feature.get('features').length; | |
var style = styleCache[size]; | |
if (!style) { | |
style = [new ol.style.Style({ | |
image: new ol.style.Circle({ | |
radius: 10, | |
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 map = new ol.Map({ | |
layers: [clusters], | |
renderer: 'canvas', | |
target: 'map', | |
view: new ol.View({ | |
center: [0, 0], | |
zoom: 2 | |
}) | |
}); | |
var e = 4500000; | |
function addFeature() { | |
var coordinates = [2 * e * Math.random() - e, 2 * e * Math.random() - e]; | |
var feature = new ol.Feature(new ol.geom.Point(coordinates)); | |
source.addFeature(feature); | |
} | |
function removeFeature() { | |
var feats = source.getFeatures(); | |
if (feats.length) { | |
var f = feats[Math.floor(Math.random() * feats.length)]; | |
source.removeFeature(f); | |
} | |
} | |
function clearFeatures() { | |
source.clear(); | |
} | |
window.setInterval(addFeature, 500); | |
window.setInterval(removeFeature, 1000); | |
window.setInterval(clearFeatures, 10000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment