forked from jonataswalker/ol-contextmenu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
202 lines (187 loc) · 7.27 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@main/dist/en/v7.0.0/legacy/ol.css"
/>
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@main/dist/en/v7.0.0/legacy/ol.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Custom Context Menu for Openlayers</title>
<style>
html,
body,
#map {
width: 100%;
height: 100%;
overflow: hidden;
}
body {
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;
}
#map {
position: absolute;
z-index: 1;
top: 0;
bottom: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<script type="module">
import ContextMenu from './src/main.ts';
const view = new ol.View({ center: [0, 0], zoom: 4 });
const vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector() });
const baseLayer = new ol.layer.Tile({ source: new ol.source.OSM() });
const map = new ol.Map({
target: 'map',
view,
layers: [baseLayer, vectorLayer],
});
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,
items,
});
map.addControl(contextmenu);
console.log({ contextmenu });
contextmenu.on('beforeopen', function (evt) {
console.log({ evt });
});
map.on('moveend', () => {
console.log('moveend', contextmenu.isOpen());
});
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) {
var coord4326 = ol.proj.transform(obj.coordinate, 'EPSG:3857', 'EPSG:4326'),
template = 'Coordinate is ({x} | {y})',
iconStyle = new ol.style.Style({
image: new ol.style.Icon({ scale: 0.6, src: pinIcon }),
text: new ol.style.Text({
offsetY: 25,
text: ol.coordinate.format(coord4326, template, 2),
font: '15px Open Sans,sans-serif',
fill: new ol.style.Fill({ color: '#111' }),
stroke: new ol.style.Stroke({ color: '#eee', width: 2 }),
}),
}),
feature = new ol.Feature({
type: 'removable',
geometry: new ol.geom.Point(obj.coordinate),
});
feature.setStyle(iconStyle);
vectorLayer.getSource().addFeature(feature);
}
</script>
</body>
</html>