Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Last active November 27, 2017 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasG77/5b7d94820da6416a6c0ade1bdbe3f745 to your computer and use it in GitHub Desktop.
Save ThomasG77/5b7d94820da6416a6c0ade1bdbe3f745 to your computer and use it in GitHub Desktop.
Points, points & points - version ol.source.Vector
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<meta name=description content="">
<meta name=viewport content="width=device-width, initial-scale=1">
<title>Points, points &amp; points - version ol.source.Vector</title>
<link rel="stylesheet" href="https://openlayers.org/en/v4.4.2/css/ol.css" type="text/css">
<!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
<script src="https://openlayers.org/en/v4.4.2/build/ol.js"></script>
<script src='https://npmcdn.com/@turf/turf/turf.min.js'></script>
<style type="text/css">
html, body {
margin: 0;
height: 100%;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
#text {
position: absolute;
z-index: 1;
bottom: 1em;
left: 1em;
padding: 15px;
background-color: white;
border: #0e0e0e 1px solid;
}
</style>
</head>
<body>
<div id="map" class="map">
<div id="text">
<h1 id="title">How Many Points...</h1>
<div id="docs">
<p>
Number of points to place on the map:
<input id="num" value="1000" type="number">
<button id="draw">Draw</button>
<button id="clear">Clear</button>
</p>
<p>
This page is an <b>OpenLayers 4</b> updated version of this <a href="https://derekswingley.com/lab/olpts/">OpenLayers 2 Derek Swingley demo</a>
<br>
It uses <a href="http://openlayers.org/en/latest/apidoc/ol.source.Vector.html">"ol.source.Vector"</a>
</p>
</div>
</div>
</div>
<script>
var vector = new ol.layer.Vector({
source: new ol.source.Vector({
format: new ol.format.GeoJSON()
}),
style: new ol.style.Style({
image: new ol.style.Circle({
radius: 6,
fill: new ol.style.Fill({
color: '#3399CC'
}),
stroke: new ol.style.Stroke({
color: '#fff',
width: 2
})
})
})
});
var raster = new ol.layer.Tile({
source: new ol.source.OSM()
})
var map = new ol.Map({
renderer: /** @type {Array<ol.renderer.Type>} */ (['webgl', 'canvas']),
layers: [
raster,
vector
],
target: 'map',
view: new ol.View({
center: [0, 0],
zoom: 2
})
});
function addRandomFeaturesInExtent(num) {
var extent = map.getView().calculateExtent();
var extent4326 = ol.proj.transformExtent(extent, 'EPSG:3857', 'EPSG:4326');
var points = turf.randomPoint(num, {
bbox: extent4326
});
vector.getSource().addFeatures(vector.getSource().getFormat().readFeatures(points, {
featureProjection: 'EPSG:3857'
}));
};
addRandomFeaturesInExtent(Number(document.getElementById('num').value));
document.getElementById('draw').addEventListener('click', function(e) {
addRandomFeaturesInExtent(Number(document.getElementById('num').value));
});
document.getElementById('clear').addEventListener('click', function(e) {
vector.getSource().clear();
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment