forked from smithmicro/mapbox-gl-circle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
172 lines (153 loc) · 6.24 KB
/
index.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'/>
<title>MapboxCircle Demo</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.1/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.1/mapbox-gl.css' rel='stylesheet'/>
<script src='https://npmcdn.com/@turf/[email protected]/turf.min.js'></script>
<script src='dist/mapbox-gl-circle.min.js'></script>
<script src='https://npmcdn.com/mapbox-gl-circle/dist/mapbox-gl-circle.min.js'></script>
<style>
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
#menu {
position: absolute;
background-color: rgba(255, 255, 255, 0.75);
padding-left: 5px;
font-family: 'Open Sans', sans-serif;
font-size: x-small;
z-index: 100;
}
#menu > label {
padding-right: 5px;
}
#version {
position: absolute;
font-family: "Consolas", monospace;
font-weight: bold;
font-size: x-small;
padding-right: 3px;
top: 0;
right: 0;
z-index: 100;
}
#info {
position: absolute;
font-family: "Consolas", monospace;
font-size: x-small;
bottom: 0;
left: 0;
right: 0;
z-index: 100;
background-color: rgba(255, 255, 255, 0.75);
}
</style>
</head>
<body>
<div id='info'>
<div style="text-align: center;">
<strong>Tips! Right-click on map to add new circle. Left-click on a circle to remove it.</strong>
</div>
<div id="debug"></div>
</div>
<div id='menu'>
<strong>Map Style:</strong>
<input id='basic' type='radio' name='styleSwitcher' value='basic'>
<label for='basic'>basic</label>
<input id='streets' type='radio' name='styleSwitcher' value='streets' checked='checked'>
<label for='streets'>streets</label>
<input id='bright' type='radio' name='styleSwitcher' value='bright'>
<label for='bright'>bright</label>
<input id='light' type='radio' name='styleSwitcher' value='light'>
<label for='light'>light</label>
<input id='dark' type='radio' name='styleSwitcher' value='dark'>
<label for='dark'>dark</label>
<input id='satellite' type='radio' name='styleSwitcher' value='satellite'>
<label for='satellite'>satellite</label>
<input id='satellite-streets' type='radio' name='styleSwitcher' value='satellite-streets'>
<label for='satellite-streets'>satellite-streets</label>
</div>
<div id='version'></div>
<div id='map'></div>
<!--suppress ES6ConvertVarToLetConst, ES6ModulesDependencies -->
<script>
document.getElementById('version').textContent = 'mapbox-gl-circle-' + MapboxCircle.VERSION;
// noinspection SpellCheckingInspection
mapboxgl.accessToken = 'pk.eyJ1IjoiYW5hbmR0aGFra2VyIiwiYSI6ImNqNWptdjU1YjJheWszM256ZWN0MXNrejMifQ.TXnoKtnlam-KBmRSjXQgEw';
function boundsTo5percentRadius(bounds) {
// noinspection JSUnresolvedVariable
// noinspection JSCheckFunctionSignatures
return Math.round(
turf.distance(bounds.getSouthWest().toArray(), bounds.getNorthEast().toArray(), 'meters') * .05);
}
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v9',
center: [18.059678, 59.322465],
zoom: 12.305430
}).addControl(new mapboxgl.ScaleControl({maxWidth: 300}), 'top-right');
var layerList = document.getElementById('menu');
var inputs = layerList.getElementsByTagName('input');
function switchLayer(clickEvent) {
var layerId = clickEvent.target.id;
map.setStyle('mapbox://styles/mapbox/' + layerId + '-v9');
}
for (var i = 0; i < inputs.length; i++) {
inputs[i].onclick = switchLayer;
}
// Non-editable circles.
var lidingoMunicipality = {position: {lat: 59.379278, lng: 18.17814}, radius: 4846};
new MapboxCircle(lidingoMunicipality.position, lidingoMunicipality.radius, {
strokeColor: '#0000ff',
strokeOpacity: 0.25,
strokeWeight: 2,
refineStroke: true,
fillColor: '#000000',
fillOpacity: 0.05
}).addTo(map);
// Editable circles.
var northPole = {position: {lat: 84.928218, lng: -38.074812}, radius: 1100000};
var irelandAndUK = {position: {lat: 54.431637, lng: -4.223538}, radius: 547293};
var oldTown = {position: {lat: 59.325017, lng: 18.069263}, radius: 508};
var smsiStockholm = {position: {lat: 59.346978, lng: 18.039120}, radius: 142};
var kvadratSthlm = {position: {lat: 59.34131, lng: 18.058817}, radius: 122};
var stormforsOffice = {position: {lat: 59.337678, lng: 18.083402}, radius: 85};
var midsommarkransenSubway = {position: {lat: 59.301857, lng: 18.012292}, radius: 131};
var editableCircleOpts = {
editable: true,
minRadius: 50,
debugEl: document.getElementById('debug')
};
var baseDelay = 500;
[northPole, irelandAndUK, oldTown, smsiStockholm, kvadratSthlm, stormforsOffice, midsommarkransenSubway].forEach(
function (item) {
window.setTimeout(function () {
var newEditable = new MapboxCircle(item.position, item.radius, editableCircleOpts).addTo(map)
.once('click', function (mapMouseEvent) { // Left-click on circle to remove.
console.log('remove circle / mapMouseEvent:', mapMouseEvent);
newEditable.remove();
});
}, baseDelay += 500);
});
map.on('contextmenu', function (event) { // Right-click on map to add new circle.
console.log('Add editable circle at ' + event.lngLat);
var newEditable = new MapboxCircle(event.lngLat, boundsTo5percentRadius(map.getBounds()), editableCircleOpts)
.once('click', function (mapMouseEvent) {
console.log('remove circle / mapMouseEvent:', mapMouseEvent);
newEditable.remove();
})
.addTo(map);
});
</script>
</body>
</html>