-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
index.html
255 lines (235 loc) · 9.2 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/ol.css" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/ol.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Custom Context Menu for Openlayers</title>
<style>
html,
body {
margin: 0;
height: 100%;
font: 1em/1.5 BlinkMacSystemFont, -apple-system, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 'Helvetica',
'Arial', sans-serif;
color: #222;
font-weight: 400;
}
.main-container {
height: 100%;
display: flex;
}
.left-container {
flex: 1 1 0;
padding: 5px;
}
.right-container {
flex: 1 1 0;
display: flex;
flex-direction: column;
padding: 5px;
}
#map1,
#map2 {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div class="main-container">
<div class="left-container">
<div id="map1"></div>
</div>
<div class="right-container">
<div id="map2"></div>
</div>
</div>
<script type="module">
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import Feature from 'ol/Feature.js';
import Point from 'ol/geom/Point.js';
import { transform } from 'ol/proj';
import { format } from 'ol/coordinate';
import { Icon, Text, Fill, Stroke, Style } from 'ol/style.js';
import { OSM, Vector as VectorSource } from 'ol/source.js';
import { Tile as TileLayer, Vector as VectorLayer } from 'ol/layer.js';
import ContextMenu from './src/main.ts';
const vectorLayer = new VectorLayer({ source: new VectorSource() });
const map = new Map({
view: new View({ center: [0, 0], zoom: 2 }),
target: 'map1',
layers: [new TileLayer({ source: new OSM() }), vectorLayer],
});
const map2 = new Map({
view: new View({ center: [0, 0], zoom: 2 }),
target: 'map2',
layers: [
new TileLayer({ source: new OSM() }),
new VectorLayer({ source: new VectorSource() }),
],
});
const pinIcon =
'https://cdn.jsdelivr.net/gh/jonataswalker/ol-contextmenu@604befc46d737d814505b5d90fc171932f747043/examples/img/pin_drop.png';
const centerIcon =
'https://cdn.jsdelivr.net/gh/jonataswalker/ol-contextmenu@604befc46d737d814505b5d90fc171932f747043/examples/img/center.png';
const listIcon =
'https://cdn.jsdelivr.net/gh/jonataswalker/ol-contextmenu@604befc46d737d814505b5d90fc171932f747043/examples/img/view_list.png';
const items = [
{
text: 'Center map here',
classname: 'bold',
icon: centerIcon,
callback: center,
},
{
text: 'Some Actions',
icon: listIcon,
items: [
{
text: 'Some more Actions',
items: [
{
text: 'Add a Marker',
icon: pinIcon,
callback: marker,
},
],
},
{
text: 'Add a Marker',
icon: pinIcon,
callback: marker,
},
{
text: 'Center map here',
icon: centerIcon,
callback: center,
},
{
text: 'Add a Marker',
icon: pinIcon,
callback: marker,
},
{
text: 'Center map here',
icon: centerIcon,
callback: center,
},
{
text: 'Add a Marker',
icon: pinIcon,
callback: marker,
},
{
text: 'Center map here',
icon: centerIcon,
callback: center,
},
{
text: 'Add a Marker',
icon: pinIcon,
callback: marker,
},
{
text: 'Center map here',
icon: centerIcon,
callback: center,
},
{
text: 'Add a Marker',
icon: pinIcon,
callback: marker,
},
],
},
{
text: 'Add a Marker',
icon: pinIcon,
callback: marker,
},
'-', // this is a separator
{
text: 'Some more Actions, loooong',
items: [
{
text: 'Add a Marker',
icon: pinIcon,
callback: marker,
},
],
},
'-', // this is a separator
];
const contextmenu = new ContextMenu({
width: 200,
defaultItems: true,
});
const contextmenu2 = new ContextMenu({
width: 200,
defaultItems: true,
});
map.addControl(contextmenu);
map2.addControl(contextmenu2);
const removeMarkerItem = {
text: 'Remove this Marker',
classname: 'marker',
callback: removeMarker,
};
contextmenu.on('open', function (evt) {
const feature = map.forEachFeatureAtPixel(evt.pixel, (ft) => ft);
if (feature && feature.get('type') === 'removable') {
contextmenu.clear();
removeMarkerItem.data = { marker: feature };
contextmenu.push(removeMarkerItem);
} else {
contextmenu.clear();
contextmenu.extend(items);
contextmenu.extend(contextmenu.getDefaultItems());
}
});
map.on('pointermove', function (e) {
if (e.dragging) return;
const pixel = map.getEventPixel(e.originalEvent);
const hit = map.hasFeatureAtPixel(pixel);
map.getTargetElement().style.cursor = hit ? 'pointer' : '';
});
function elastic(t) {
return Math.pow(2, -10 * t) * Math.sin(((t - 0.075) * (2 * Math.PI)) / 0.3) + 1;
}
function center(obj) {
view.animate({
duration: 700,
easing: elastic,
center: obj.coordinate,
});
}
function removeMarker(obj) {
vectorLayer.getSource().removeFeature(obj.data.marker);
}
function marker(obj) {
const coord4326 = transform(obj.coordinate, 'EPSG:3857', 'EPSG:4326'),
template = 'Coordinate is ({x} | {y})',
iconStyle = new Style({
image: new Icon({ scale: 0.6, src: pinIcon }),
text: new Text({
offsetY: 25,
text: format(coord4326, template, 2),
font: '15px Open Sans,sans-serif',
fill: new Fill({ color: '#111' }),
stroke: new Stroke({ color: '#eee', width: 2 }),
}),
}),
feature = new Feature({
type: 'removable',
geometry: new Point(obj.coordinate),
});
feature.setStyle(iconStyle);
vectorLayer.getSource().addFeature(feature);
}
</script>
</body>
</html>