作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为谷歌地图创建一个自定义上下文菜单,该菜单仅在我单击标记时才有效,但我正在使用的此上下文菜单将在我单击的任何位置显示上下文菜单。我想专门为 map 标记制作上下文菜单。
HTML:
<div id="map" style="width:100%;height:500px"></div>
<div class="menu" id="menu">
<div class="menu-item"><i class="glyphicon glyphicon-file"></i>Manage files</div>
</div>
JS:
function myMap() {
var menuDisplayed = false;
var menuBox = null;
var myCenter = new google.maps.LatLng(51.508742,-0.120850);
var mapCanvas = document.getElementById("map");
var mapOptions = {center: myCenter, zoom: 5};
var map = new google.maps.Map(mapCanvas, mapOptions);
var marker = new google.maps.Marker({position:myCenter});
marker.setMap(map);
window.addEventListener("contextmenu", function() {
var left = arguments[0].clientX;
var top = arguments[0].clientY;
menuBox = document.getElementById("menu");
menuBox.style.left = left + "px";
menuBox.style.top = top + "px";
menuBox.style.display = "block";
arguments[0].preventDefault();
menuDisplayed = true;
}, false);
window.addEventListener("click", function() {
if(menuDisplayed == true){
menuBox.style.display = "none";
}
}, true);
}
CSS:
.menu {
width: 150px;
box-shadow: 3px 3px 5px #888888;
border-style: solid;
border-width: 1px;
border-color: grey;
border-radius: 2px;
position: fixed;
display: none;
}
.menu-item {
height: 20px;
background-color: white;
}
.menu-item:hover {
background-color: #6CB5FF;
cursor: pointer;
}
最佳答案
一个选项不是捕获对 window
对象的所有点击,而是捕获标记上的 rightclick
,打开上下文菜单,在单击 map 。
marker.addListener("rightclick", function(e) {
for (prop in e) {
if (e[prop] instanceof MouseEvent) {
mouseEvt = e[prop];
var left = mouseEvt.clientX;
var top = mouseEvt.clientY;
menuBox = document.getElementById("menu");
menuBox.style.left = left + "px";
menuBox.style.top = top + "px";
menuBox.style.display = "block";
mouseEvt.preventDefault();
menuDisplayed = true;
}
}
});
map.addListener("click", function(e) {
if (menuDisplayed == true) {
menuBox.style.display = "none";
}
});
代码片段:
function myMap() {
var menuDisplayed = false;
var menuBox = null;
var myCenter = new google.maps.LatLng(51.508742, -0.120850);
var mapCanvas = document.getElementById("map");
var mapOptions = {
center: myCenter,
zoom: 5
};
var map = new google.maps.Map(mapCanvas, mapOptions);
var marker = new google.maps.Marker({
position: myCenter
});
marker.setMap(map);
marker.addListener("rightclick", function(e) {
for (prop in e) {
if (e[prop] instanceof MouseEvent) {
mouseEvt = e[prop];
var left = mouseEvt.clientX;
var top = mouseEvt.clientY;
menuBox = document.getElementById("menu");
menuBox.style.left = left + "px";
menuBox.style.top = top + "px";
menuBox.style.display = "block";
mouseEvt.preventDefault();
menuDisplayed = true;
}
}
});
map.addListener("click", function(e) {
if (menuDisplayed == true) {
menuBox.style.display = "none";
}
});
}
google.maps.event.addDomListener(window, "load", myMap);
html,
body,
#map {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
.menu {
width: 150px;
box-shadow: 3px 3px 5px #888888;
border-style: solid;
border-width: 1px;
border-color: grey;
border-radius: 2px;
position: fixed;
display: none;
}
.menu-item {
height: 20px;
background-color: white;
}
.menu-item:hover {
background-color: #6CB5FF;
cursor: pointer;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map"></div>
<div class="menu" id="menu">
<div class="menu-item"><i class="glyphicon glyphicon-file"></i>Manage files</div>
</div>
关于javascript - 谷歌地图标记自定义上下文菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51747771/
leaflet:一个开源并且对移动端友好的交互式地图 JavaScript 库 中文文档: https://leafletjs.cn/reference.html 官网(英文): ht
我是一名优秀的程序员,十分优秀!