-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmesurement.html
157 lines (137 loc) · 4.34 KB
/
mesurement.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Measure distances</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.2/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.2/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<style>
.distance-container {
position: absolute;
top: 10px;
left: 10px;
z-index: 1;
}
.distance-container > * {
background-color: rgba(0, 0, 0, 0.5);
color: #fff;
font-size: 11px;
line-height: 18px;
display: block;
margin: 0;
padding: 5px 10px;
border-radius: 3px;
}
</style>
<div id='map'></div>
<div id='distance' class='distance-container'></div>
<script src='https://api.mapbox.com/mapbox.js/plugins/turf/v2.0.2/turf.min.js'></script>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoiZGV2MmF0d29yayIsImEiOiJjamdid29jZTExNmdtMndtcXlmdG1pcjN6In0.5RStY9ckXTAxVJK3SFL45w';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/dev2atwork/cjget7ry900232rqp03wabv4g',
center: [2.3399, 48.8555],
zoom: 12
});
var distanceContainer = document.getElementById('distance');
// GeoJSON object to hold our measurement features
var geojson = {
"type": "FeatureCollection",
"features": []
};
// Used to draw a line between points
var linestring = {
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": []
}
};
map.on('load', function() {
map.addSource('geojson', {
"type": "geojson",
"data": geojson
});
// Add styles to the map
map.addLayer({
id: 'measure-points',
type: 'circle',
source: 'geojson',
paint: {
'circle-radius': 5,
'circle-color': '#000'
},
filter: ['in', '$type', 'Point']
});
map.addLayer({
id: 'measure-lines',
type: 'line',
source: 'geojson',
layout: {
'line-cap': 'round',
'line-join': 'round'
},
paint: {
'line-color': '#000',
'line-width': 2.5
},
filter: ['in', '$type', 'LineString']
});
map.on('click', function(e) {
var features = map.queryRenderedFeatures(e.point, { layers: ['measure-points'] });
// Remove the linestring from the group
// So we can redraw it based on the points collection
if (geojson.features.length > 1) geojson.features.pop();
// Clear the Distance container to populate it with a new value
distanceContainer.innerHTML = '';
// If a feature was clicked, remove it from the map
if (features.length) {
var id = features[0].properties.id;
geojson.features = geojson.features.filter(function(point) {
return point.properties.id !== id;
});
} else {
var point = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
e.lngLat.lng,
e.lngLat.lat
]
},
"properties": {
"id": String(new Date().getTime())
}
};
geojson.features.push(point);
}
if (geojson.features.length > 1) {
linestring.geometry.coordinates = geojson.features.map(function(point) {
return point.geometry.coordinates;
});
geojson.features.push(linestring);
// Populate the distanceContainer with total distance
var value = document.createElement('pre');
value.textContent = 'Total distance: ' + turf.lineDistance(linestring).toLocaleString() + 'km';
distanceContainer.appendChild(value);
}
map.getSource('geojson').setData(geojson);
});
});
map.on('mousemove', function (e) {
var features = map.queryRenderedFeatures(e.point, { layers: ['measure-points'] });
// UI indicator for clicking/hovering a point on the map
map.getCanvas().style.cursor = (features.length) ? 'pointer' : 'crosshair';
});
</script>
</body>
</html>