-
Notifications
You must be signed in to change notification settings - Fork 12
/
measurement-along.html
104 lines (100 loc) · 3.53 KB
/
measurement-along.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://rawcdn.githack.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" type="text/css">
<link rel="stylesheet" href="assets/css/styles.css" type="text/css">
<style type="text/css">
#length {
width: 300px;
}
</style>
<script src="//cdn.jsdelivr.net/npm/@turf/turf@5/turf.min.js"></script>
<!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
<script src="http://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
<script src="https://rawcdn.githack.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
<title>Turf and OpenLayers - Along</title>
</head>
<body>
<div id="map" class="map"></div>
<input id="length" type="text" name="input" placeholder="Set distance from Nantes (in kilometers)">
<button id="validate" type="button">Validate</button>
<script type="text/javascript">
var geojson;
var length = document.getElementById('length');
var button = document.getElementById('validate');
button.addEventListener('click', function(e) {
var inputValue = length.value;
if (inputValue.match(/^\d+$/)) {
var along = turf.along(geojson.features[0], +inputValue, {
units: 'kilometers'
});
console.log(along);
var featuresPt = (new ol.format.GeoJSON()).readFeatures(along, {
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857'
});
vectorLayerPoint.getSource().clear();
vectorLayerPoint.getSource().addFeatures(featuresPt);
} else {
console.log('The input is invalid!');
}
});
// Declare an empty layer for point
var vectorLayerPoint = new ol.layer.Vector({
source: new ol.source.Vector()
});
// Declare an empty layer for a line
var vectorLayerLine = new ol.layer.Vector({
source: new ol.source.Vector(),
style: [
new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'white',
width: 5
})
}),
new ol.style.Style({
stroke: new ol.style.Stroke({
color: '#007cc3',
width: 3
})
})
]
});
// Instanciate a map and add layers
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
vectorLayerLine,
vectorLayerPoint
],
view: new ol.View({
center: ol.proj.transform(
[3.768, 48.879],
'EPSG:4326',
'EPSG:3857'
),
zoom: 6
})
});
fetch('assets/data/from-nantes-to-bruxelles-osrm.geojson').then(function(response) {
return response.json();
}).then(function(json) {
// To expose the geojson content to the outside
geojson = json;
// Serialize features from GeoJSON to array of ol.Feature
var features = (new ol.format.GeoJSON()).readFeatures(json, {
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857'
});
// Add features to the line layer source
vectorLayerLine.getSource().addFeatures(features);
}).catch(function(ex) {
console.log('parsing failed', ex);
});
</script>
</body>
</html>