Skip to content

Commit cf89bc6

Browse files
committed
Update example
1 parent 18e76e4 commit cf89bc6

7 files changed

+67
-71
lines changed

examples/contextmenu.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ body {
2525
.marker {
2626
text-indent: 20px;
2727
background-size: 20px auto;
28-
background-image: url("img/marker.png");
28+
background-image: url("img/pin_drop.png");
2929
background-repeat: no-repeat;
3030
background-position: left center;
3131
}
3232

3333
li.bold {
3434
font-weight: 700;
35-
}
35+
}

examples/contextmenu.js

+65-69
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,89 @@
1-
(function(win, doc){
1+
(function(){
22
'use strict';
33

4-
var
5-
view = new ol.View({
6-
center: [0, 0],
7-
zoom: 3,
8-
minZoom: 2,
9-
maxZoom: 20
10-
}),
11-
vectorLayer = new ol.layer.Vector({
12-
source: new ol.source.Vector()
13-
}),
14-
baseLayer = new ol.layer.Tile({
15-
preload: Infinity,
16-
opacity: 1,
17-
source: new ol.source.MapQuest({layer: 'osm'})
18-
}),
19-
map = new ol.Map({
20-
target: doc.getElementById('map'),
21-
loadTilesWhileAnimating: true,
22-
view: view,
23-
layers: [baseLayer, vectorLayer]
24-
}),
25-
// from https://github.com/DmitryBaranovskiy/raphael
26-
elastic = function(t) {
27-
return Math.pow(2, -10 * t) * Math.sin((t - 0.075) * (2 * Math.PI) / 0.3) + 1;
28-
},
29-
center = function(obj){
30-
var pan = ol.animation.pan({
31-
duration: 1000,
32-
easing: elastic,
33-
source: view.getCenter()
34-
});
35-
map.beforeRender(pan);
36-
view.setCenter(obj.coordinate);
37-
},
38-
marker = function(obj){
39-
var
40-
coord4326 = ol.proj.transform(obj.coordinate, 'EPSG:3857', 'EPSG:4326'),
41-
template = 'Coordinate is ({x} | {y})',
42-
iconStyle = new ol.style.Style({
43-
image: new ol.style.Icon({
44-
scale: .6,
45-
src: 'img/marker.png'
46-
}),
47-
text: new ol.style.Text({
48-
offsetY: 25,
49-
text: ol.coordinate.format(coord4326, template, 2),
50-
font: '15px Open Sans,sans-serif',
51-
fill: new ol.style.Fill({ color: '#111' }),
52-
stroke: new ol.style.Stroke({
53-
color: '#eee', width: 2
54-
})
55-
})
56-
}),
57-
feature = new ol.Feature({
58-
type: 'removable',
59-
geometry: new ol.geom.Point(obj.coordinate)
4+
var view = new ol.View({
5+
center: [0, 0],
6+
zoom: 4,
7+
minZoom: 2,
8+
maxZoom: 20
9+
}),
10+
vectorLayer = new ol.layer.Vector({
11+
source: new ol.source.Vector()
12+
}),
13+
baseLayer = new ol.layer.Tile({
14+
source: new ol.source.OSM()
15+
}),
16+
map = new ol.Map({
17+
target: document.getElementById('map'),
18+
view: view,
19+
layers: [baseLayer, vectorLayer]
20+
}),
21+
// from https://github.com/DmitryBaranovskiy/raphael
22+
elastic = function(t) {
23+
return Math.pow(2, -10 * t) * Math.sin((t - 0.075) * (2 * Math.PI) / 0.3) + 1;
24+
},
25+
center = function(obj){
26+
var pan = ol.animation.pan({
27+
duration: 1000,
28+
easing: elastic,
29+
source: view.getCenter()
6030
});
61-
62-
feature.setStyle(iconStyle);
63-
vectorLayer.getSource().addFeature(feature);
64-
};
31+
map.beforeRender(pan);
32+
view.setCenter(obj.coordinate);
33+
},
34+
marker = function(obj){
35+
var coord4326 = ol.proj.transform(obj.coordinate, 'EPSG:3857', 'EPSG:4326'),
36+
template = 'Coordinate is ({x} | {y})',
37+
iconStyle = new ol.style.Style({
38+
image: new ol.style.Icon({
39+
scale: .6,
40+
src: 'img/pin_drop.png'
41+
}),
42+
text: new ol.style.Text({
43+
offsetY: 25,
44+
text: ol.coordinate.format(coord4326, template, 2),
45+
font: '15px Open Sans,sans-serif',
46+
fill: new ol.style.Fill({ color: '#111' }),
47+
stroke: new ol.style.Stroke({
48+
color: '#eee', width: 2
49+
})
50+
})
51+
}),
52+
feature = new ol.Feature({
53+
type: 'removable',
54+
geometry: new ol.geom.Point(obj.coordinate)
55+
});
56+
57+
feature.setStyle(iconStyle);
58+
vectorLayer.getSource().addFeature(feature);
59+
};
6560

6661
var contextmenu_items = [
6762
{
6863
text: 'Center map here',
6964
classname: 'bold',
65+
icon: 'img/center.png',
7066
callback: center
7167
},
7268
{
7369
text: 'Some Actions',
70+
icon: 'img/view_list.png',
7471
items: [
7572
{
7673
text: 'Center map here',
74+
icon: 'img/center.png',
7775
callback: center
7876
},
7977
{
80-
text: 'Center map here',
81-
callback: center
78+
text: 'Add a Marker',
79+
icon: 'img/pin_drop.png',
80+
callback: marker
8281
}
8382
]
8483
},
8584
{
8685
text: 'Add a Marker',
87-
icon: 'img/marker.png',
86+
icon: 'img/pin_drop.png',
8887
callback: marker
8988
},
9089
'-' // this is a separator
@@ -106,7 +105,6 @@
106105
callback: removeMarker
107106
};
108107

109-
var changed = false;
110108
contextmenu.on('open', function(evt){
111109
var feature = map.forEachFeatureAtPixel(evt.pixel, function(ft, l){
112110
return ft;
@@ -117,12 +115,10 @@
117115
marker: feature
118116
};
119117
contextmenu.push(removeMarkerItem);
120-
changed = true;
121-
} else if (changed) {
118+
} else {
122119
contextmenu.clear();
123120
contextmenu.extend(contextmenu_items);
124121
contextmenu.extend(contextmenu.getDefaultItems());
125-
changed = false;
126122
}
127123
});
128124

@@ -135,4 +131,4 @@
135131
map.getTarget().style.cursor = hit ? 'pointer' : '';
136132
});
137133

138-
})(window, document);
134+
})();

examples/img/anchor.png

-666 Bytes
Binary file not shown.

examples/img/center.png

846 Bytes
Loading

examples/img/marker.png

-4.1 KB
Binary file not shown.

examples/img/pin_drop.png

905 Bytes
Loading

examples/img/view_list.png

380 Bytes
Loading

0 commit comments

Comments
 (0)