How to disable "close all popups" in MapBox? - mapbox

Not too long ago, people had a really hard time getting all popups to close on map click in mapbox (How to close all popups programmatically in mapbox gl?).
They've since changed this to the default behavior, which is nice, but does not suit my current project.
How does one NOT have all popups close on click out?
And once that is disabled:
How does one programmatically close selected popups on click out?

Like this:
const popup = new mapboxgl.Popup({ closeOnClick: false });
map.on('click', () => popup.remove());

Related

leaflet popup auto close upon api call

I am using leaflet to show my markers into google map. everything works fine. My markers are being loaded by an api call in every 10 secs. if i click on any markers a popup window is open. But it disappeared as soon as the api is called again. I have tried so far as follows:
var infoWindow_content = "<h3>Hello world</h3>";
var theMarker = L.marker([devices[x].lat, devices[x].lng], {
icon: customicon,
rotationAngle: devices[x].angle
}).on('click', markerOnClick).addTo(map).bindPopup(infoWindow_content, {
autoPan: true,
autoClose:false,
closeOnClick:false,
});
is there anyway to keep the popup window open even the marker gets reloaded? In that case is the popup window follow marker movement? thanks in advance

Keeping mapboxgl-js popup open with listeners bound to mouse movement

I am drawing a map with routes (LineString) on them and I am handling
mouse hover (through setFeatureState) to make them look thicker
mouse click - to select a route and show some information outside of the map
In a case where two routes are detected under a click (e.features.length > 1) I want to show a new Popup({ closeButton: true }), with disambiguation info/links so that user can precisely select the route they want.
Unfortunately, tracking the mouse movement causes the popup to disappear when user tries to enter the popup area to click the links within.
So what I am doing is, I am unbinding the mouse move listeners before showing the popup, and re-binding on close
showPopup(mapbox){
this.removeListeners(mapbox);
const popup = new mapboxgl.Popup({
closeButton: true,
closeOnClick: true,
});
popup.once('close', () => {
this.addListeners(mapbox);
});
}
Is there any better API way to solve that?

Add url redirect to mapbox icon

I am trying to allow a mapbox marker to be clicked on and when clicked it automatically takes you to a new link.
Is this possible?
I currently have a map of 10 locations and when loaded the zoom level shows all. When you click on a location, it zooms you into that location.
I now want it to take you through to a url on the click rather than zoom in, however I cant seem to find any documentation on how to do it.
I am aware that it can be done using a popup box which contains a url in it, but is there a way to remove the extra step.
Thank you
You can use click event on your layer to get the feature clicked and use a property of your feature to build your link :
map.on('click', 'layername', function(e) {
// Here you can access e.features[0] which is the feature cliked
// With that you can do whatever you want with your feature
});
Sébastien Bousquet's answer work when using a Symbol, but if using a Marker, you'll need to add your your own click eventlistener like https://developer.mozilla.org/en-US/docs/Web/API/Element/click_event.
marker.getElement().addEventListener('click', event => {
window.location.href = 'https://www.mapbox.com/';
});

Leaflet layer control open only on click

Is there any way to open leaflet layer control only when clicked?
By default, it expands/collapse when on mouseover/mouseout. I want to open only on click.
You can use a bit of jQuery to get this done.
Set the 'collapsed' option to false and instead, create a button to show/hide the layer control.
btn.onclick = function() {
$('.leaflet-control-layers').toggle();
}
jsFiddle:https://jsfiddle.net/jht7u28L/1/ (a basic example)
Stop propagation on mouse over solved it. I am using d3 here but It can be easily handled by plain javascript or by jQuery.
d3.select(".leaflet-control-layers-toggle").on("mouseover", function () {
//this will make sure that layer popup menu
//not opens when mouseover
d3.event.stopPropagation();
});

Open leaflet marker popup-link in a new window

I have a problem using links in leaflet marker popups:
My loop assigns a link to every popup so I can open the specific source by clicking the popup
var link = "Grafik erstellen";
L.marker([lat,lon], {icon: marker}).bindPopup(link).addTo(map);
It´s possible to open the link which appears in the popup, but I need to open it in a new (popup) window (simulate the windows-function of right-mouse-click: "open in new tab")
How can I realize this issue?
Thanks a lot for help
try this, add the target="_blank" attribute to the a href=""...-tag:
var link = "Grafik erstellen";
L.marker([lat,lon], {icon: marker}).bindPopup(link).addTo(map);