我有一个具有全屏控件的mapbox地图。
我想使其动态化,以便当用户A单击时,它就可以正常工作,但当用户B单击时,它会发出JavaScript警报,并显示诸如“您好,您没有高级帐户可全屏显示。请升级”,什么也没发生。
我该怎么做?
var mapboxMap = new mapboxgl.Map({
container: 'map-container',
style: styleUrl,
center: [lng, lat],
zoom: zoomVal
});
mapboxMap.addControl(new mapboxgl.FullscreenControl());
由地图库创建的按钮:
<button class="mapboxgl-ctrl-icon mapboxgl-ctrl-fullscreen" type="button" aria-label="Enter fullscreen" title="Enter fullscreen"></button>
尝试1:
$('.mapboxgl-ctrl-fullscreen').click(function(event) {
event.stopPropagation();
if (user == 'user-B') {
alert('my message');
}
})
2:
mapboxMap.on('click', function(event) {
event.stopPropagation();
if (user == 'user-B') {
alert('my message');
}
})
3:
mapboxmap._fullscreencontrol.click(function(event) {.........})
4:
var fsc = new mapboxgl.FullscreenControl();
mapboxMap.addControl(fsc);
fsc._onClickFullscreen(function(event) {.........});
什么都没有。
答案 0 :(得分:2)
由于Mapbox是在加载初始DOM后添加按钮的,因此您很有可能必须将侦听器附加到父元素(如document
),并在事件从您所使用的任何元素冒出来时捕获事件点击。您可以使用jQuery轻松做到这一点:
$(document).on('click', '.mapboxgl-ctrl-fullscreen', function(event) {
if (user == 'user-B') {
alert('my message');
}
});
进一步阅读
答案 1 :(得分:1)
您可以将if / else实际添加到地图上。看起来可能像这样:
if (userA) {
mapboxMap.addControl(new mapboxgl.FullscreenControl());
} else if (userB) {
// do something else
} else {
// do something with error
}
答案 2 :(得分:0)
事实证明,劫持全屏控件是不可能的,相反,我有了一些想法来创建一个看起来像全屏多边形的自定义控件,并使用了它:
共享在这里,以便将来其他任何人都可以使用它。
1-创建了一个支持自定义绘图的类。
class MapboxCustomControl {
constructor({
className = "",
title = "",
eventHandler = evtHndlr
}) {
this._className = className;
this._title = title;
this._eventHandler = eventHandler;
}
onAdd(map) {
this._btn = document.createElement("button");
this._btn.className = "mapboxgl-ctrl-icon" + " " + this._className;
this._btn.type = "button";
this._btn.title = this._title;
this._btn.onclick = this._eventHandler;
this._container = document.createElement("div");
this._container.className = "mapboxgl-ctrl-group mapboxgl-ctrl";
this._container.appendChild(this._btn);
return this._container;
}
}
2-添加了一个CSS样式类来绘制多边形。
.mapbox-gl-draw_polygon {
background-repeat: no-repeat;
background-position: center;
pointer-events: auto;
background-image: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PHN2ZyAgIHhtbG5zOmRjPSJodHRwOi8vcHVybC5vcmcvZGMvZWxlbWVudHMvMS4xLyIgICB4bWxuczpjYz0iaHR0cDovL2NyZWF0aXZlY29tbW9ucy5vcmcvbnMjIiAgIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyIgICB4bWxuczpzdmc9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiAgIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgICB4bWxuczpzb2RpcG9kaT0iaHR0cDovL3NvZGlwb2RpLnNvdXJjZWZvcmdlLm5ldC9EVEQvc29kaXBvZGktMC5kdGQiICAgeG1sbnM6aW5rc2NhcGU9Imh0dHA6Ly93d3cuaW5rc2NhcGUub3JnL25hbWVzcGFjZXMvaW5rc2NhcGUiICAgd2lkdGg9IjIwIiAgIGhlaWdodD0iMjAiICAgdmlld0JveD0iMCAwIDIwIDIwIiAgIGlkPSJzdmcxOTE2NyIgICB2ZXJzaW9uPSIxLjEiICAgaW5rc2NhcGU6dmVyc2lvbj0iMC45MStkZXZlbCtvc3htZW51IHIxMjkxMSIgICBzb2RpcG9kaTpkb2NuYW1lPSJzcXVhcmUuc3ZnIj4gIDxkZWZzICAgICBpZD0iZGVmczE5MTY5IiAvPiAgPHNvZGlwb2RpOm5hbWVkdmlldyAgICAgaWQ9ImJhc2UiICAgICBwYWdlY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ncm91bmQ6YWNjdW11bGF0ZSIgICAgICAgaWQ9InBhdGg0MzcyIiAgICAgICBjeD0iMTQiICAgICAgIGN5PSIxMDM4LjM2MjIiICAgICAgIHI9IjIiIC8+ICA8L2c+PC9zdmc+);
}
3-添加了处理我其他任务的功能。
function openPrivacy(event) {
if (event.type == 'click') {
var mapWindow = window.open("/private_page/" + event_uuid +"/", "private_page", 'height=800px,width=900');
if (window.focus) {
mapWindow.focus()
}
}
}
4-最后,向地图添加了自定义控件。
const customControl = new MapboxCustomControl({
className: "mapbox-gl-draw_polygon",
title: "Privacy Page",
eventHandler: openPrivacy
});
mapboxMap.addControl(customControl, "top-right");
感谢其他奉献时间的人。