Synthetic points example.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Synthetic Points</title>
<!-- 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>
<style>
.map {
width: 100%;
height:400px;
}
</style>
</head>
<body>
<div id="map" class="map"></div>
<script src="index.js"></script>
</body>
</html>
import 'ol/ol.css';
import Feature from 'ol/Feature';
import Map from 'ol/Map';
import View from 'ol/View';
import {LineString, Point} from 'ol/geom';
import VectorLayer from 'ol/layer/Vector';
import VectorSource from 'ol/source/Vector';
import {Circle as CircleStyle, Fill, Stroke, Style} from 'ol/style';
import {getVectorContext} from 'ol/render';
var count = 20000;
var features = new Array(count);
var e = 18000000;
for (var i = 0; i < count; ++i) {
features[i] = new Feature({
'geometry': new Point(
[2 * e * Math.random() - e, 2 * e * Math.random() - e]),
'i': i,
'size': i % 2 ? 10 : 20
});
}
var styles = {
'10': new Style({
image: new CircleStyle({
radius: 5,
fill: new Fill({color: '#666666'}),
stroke: new Stroke({color: '#bada55', width: 1})
})
}),
'20': new Style({
image: new CircleStyle({
radius: 10,
fill: new Fill({color: '#666666'}),
stroke: new Stroke({color: '#bada55', width: 1})
})
})
};
var vectorSource = new VectorSource({
features: features,
wrapX: false
});
var vector = new VectorLayer({
source: vectorSource,
style: function(feature) {
return styles[feature.get('size')];
}
});
var map = new Map({
layers: [vector],
target: document.getElementById('map'),
view: new View({
center: [0, 0],
zoom: 2
})
});
var point = null;
var line = null;
var displaySnap = function(coordinate) {
var closestFeature = vectorSource.getClosestFeatureToCoordinate(coordinate);
if (closestFeature === null) {
point = null;
line = null;
} else {
var geometry = closestFeature.getGeometry();
var closestPoint = geometry.getClosestPoint(coordinate);
if (point === null) {
point = new Point(closestPoint);
} else {
point.setCoordinates(closestPoint);
}
if (line === null) {
line = new LineString([coordinate, closestPoint]);
} else {
line.setCoordinates([coordinate, closestPoint]);
}
}
map.render();
};
map.on('pointermove', function(evt) {
if (evt.dragging) {
return;
}
var coordinate = map.getEventCoordinate(evt.originalEvent);
displaySnap(coordinate);
});
map.on('click', function(evt) {
displaySnap(evt.coordinate);
});
var stroke = new Stroke({
color: 'rgba(255,255,0,0.9)',
width: 3
});
var style = new Style({
stroke: stroke,
image: new CircleStyle({
radius: 10,
stroke: stroke
})
});
vector.on('postrender', function(evt) {
var vectorContext = getVectorContext(evt);
vectorContext.setStyle(style);
if (point !== null) {
vectorContext.drawGeometry(point);
}
if (line !== null) {
vectorContext.drawGeometry(line);
}
});
map.on('pointermove', function(evt) {
if (evt.dragging) {
return;
}
var pixel = map.getEventPixel(evt.originalEvent);
var hit = map.hasFeatureAtPixel(pixel);
if (hit) {
map.getTarget().style.cursor = 'pointer';
} else {
map.getTarget().style.cursor = '';
}
});
{
"name": "synthetic-points",
"dependencies": {
"ol": "6.1.1"
},
"devDependencies": {
"parcel": "1.11.0"
},
"scripts": {
"start": "parcel index.html",
"build": "parcel build --experimental-scope-hoisting --public-url . index.html"
}
}