forked from lzugis/openlayers3-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contextmenu.html
81 lines (72 loc) · 2.01 KB
/
contextmenu.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
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>contextmenu</title>
<link rel="stylesheet" href="https://openlayers.org/en/v4.0.1/css/ol.css" type="text/css">
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" type="text/css">
<style type="text/css">
.contextmenu{
position: absolute;
top: 100%;
width: 120px;
height:auto;
z-index: 9;
background-color: #ffffff;
}
.contextmenu ul{
width: 100%;
padding: 6px 2px 0 2px;
list-style: none;
}
.contextmenu > ul > li{
width: 100%;
text-align: center;
padding: 5px 0;
}
.contextmenu > ul > li:hover{
background-color: rgba(255, 0, 0, 0.5);
}
</style>
</head>
<body style="margin:0 0 0 0; padding:0 0 0 0;">
<div id="contextmenu_container" class="contextmenu">
<ul>
<li><a href="#">设置中心</a></li>
<li><a href="#">添加地标</a></li>
<li><a href="#">距离丈量</a></li>
</ul>
</div>
<div id="map"></div>
<script type="text/javascript" src="scripts/libs/jquery.js"></script>
<script type="text/javascript" src="scripts/libs/ol.js"></script>
<script type="text/javascript">
var map = new ol.Map({
target: document.getElementById("map"),
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: new ol.View({
center: [12977452.2662, 4847790.3422],
zoom: 9
})
});
var menu_overlay = new ol.Overlay({
element: document.getElementById("contextmenu_container"),
positioning: 'center-center'
});
menu_overlay.setMap(map);
$(map.getViewport()).on("contextmenu", function(e){
e.preventDefault();
var coordinate = map.getEventCoordinate(e);
menu_overlay.setPosition(coordinate);
});
$(map.getViewport()).on("click", function(e){
e.preventDefault();
menu_overlay.setPosition(undefined);
});
</script>
</body>
</html>