How to create a custom settings control in Leaflet - leaflet

I would like to add a custom container to Leaflet. The container would contain edit controls and would be used as a kind of properties editor in order to customize the map (marker colors, zoom level, polyline color, etc ...). The panel would be displayed when the user clicks on a "settings" button located on the map.
Is there a Leaflet plugin for this?
I also had a look at how to implement custom controls, but I am really not clear how to achieve this. In particular it seems to me that I can only use JavaScript and DOM manipulations (and no direct HTML markup) in order to create a custom control.
Could someone please help me bootstrap the control? thanks!
Edit:
So I tried to create a very simple container consisting of a single checkbox "control" as follow:
L.Control.SettingsPanel = L.Control.extend({
onAdd: function(map){
var checkbox = L.DomUtil.create('input');
checkbox.setAttribute("type", "checkbox");
checkbox.style.width = '200px';
return checkbox;
}
});
L.control.settingsPanel = function(opts){
return new L.Control.SettingsPanel(opts);
}

The sidebar v2 leaflet plugin might be what you are looking for.

Related

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 image instead of popup : leaflet

I am working with leaflet api.I can draw rectangles and polygons.I have bound the popup with every rectangle and polygon.When i click on drawn shape, popup opens(leaflet functionality).Popup contains some html(image).
As i am working on a demo application, i am wiling to try the fancebox plugin.
Means, when i click on drawn shape, instead of popup, i want to open up that image using fancybox.
Can i do that using simple method like using another function instead of .bindpopup.
Working Script (image loaded using fance box when we click on popup)
e.layer.bindPopup("<a class='fancybox' rel='group' href=''><img /></a>");
I can understand there must be some other javascript function to do it.
If there is some way to do it please let me know, as i am new to leaflet didn't have enough mental power to understand it yet but i hope i will....
Thanks for your time :)
I would just do e.layer.on('click', function() { //do fancybox init, perhaps like $(body).append("<a class='fancybox' rel='group' href=''><img /></a>")})
Although it makes a lot more performance sense to bind that event on the L.FeatureGroup holding all the shapes instead of one by one.

Leaflet Multipolygon custom popup

I have an OSM map and I'm using leafletjs.
I have created my custom popup for marker. It it works fine and correctly.
marker.bindPopup(strMsg,{className: 'myPopup'});
This code works perfectly.
Now, I want to create a the same popup, but clicking on Multilopygon. The data for the polygon comes from geoJSON. This is the code I wrote for this issue
var c_park = L.geoJson(data[i].geom, {
style: myStyle
});
c_park.bindPopup("strMsg",{className: 'myPopup'});
map.addLayer(c_park);
The problem is myPopup class is not working for the popup of multipolygon and as a result I get the native popup window. If I manually change the class in browser - it is ok.
I tried different methods. F.e. using function onEachFeature to init popups. But nothing works.
Does anybody knows what is the problem ?
Does anybody knows what is the problem ?
There is no problem. ClassName is supported for markers as an option of L.icon. (docs).
Polygon inherits the options of polyline and path, which do not include ClassName (docs).
The way I see it you have two possibilities:
fork leaflet and add the className option to polygon
make a subclass inheriting polygon taking className as an option by overloading bindPopup

Bing Maps autoadjusting infobox for its content

I am adding bing maps to one of our sites. I am able to show pushpins and infoboxes.
The problem I am facing now is the infobox content go out of the white box.
Is there a way to have autoadjusting infoboxes for bing maps or its CSS?
Fixed using Jquery:
function displayInfobox(e) {
pinInfobox.setOptions({description: e.target.Description, visible:true, offset: new Microsoft.Maps.Point(0,4)});
pinInfobox.setLocation(e.target.getLocation());
map.setView({center:e.target.getLocation(),zoom:4});
adjustHeightInfobox();
}
function adjustHeightInfobox() {
var newHeight = jQuery(".infobox-info").height();
pinInfobox.setOptions({height:newHeight+15});
}
Assuming that you're using the default infobox within the AJAX control, you will be able to use the infoboxOption class where you can easily specify the width and height of the infobox.
See the MSDN for all options available on this specific class: http://msdn.microsoft.com/en-us/library/gg675210.aspx
If you want to adapt the infobox to your own needs (i.e. dynamic size), you can use your own HTML content and customize is with your own CSS, see: http://www.bingmapsportal.com/isdk/ajaxv7#Infobox14