Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

leafletjs marker bindpopup() with options

Tags:

The leaflet documention shows you can add a popup to a marker with

marker.bindPopup("<b>Hello world!</b><br>I am a popup.").openPopup(); 

or create a standalone popup with

var popup = L.popup()     .setLatLng([51.5, -0.09])     .setContent("I am a standalone popup.")     .openOn(map); 

Is there no way to set popup options and bind it to a marker? I want to be able to set my own maxwidth for popups and have them open/close when you click a marker.

like image 724
nexus_6 Avatar asked May 26 '14 16:05

nexus_6


People also ask

How do I add a marker in leaflet JS?

LeafletJS - Markers 1 Adding a Simple Marker. Step 1 − Create a Map object by passing a < div > element (String or object) and map options (optional). 2 Binding Pop-ups to the Marker. Step 1 − Create a Map object by passing a < div > element (String or object) and map options (optional). 3 Marker Options. ... 4 Marker Custom Icons. ...

How do I bind a popup to a leaflet?

Are you sure that you're reading the Leaflet reference documentation? It specifies that you can bind a popup with options by creating it and calling .bindPopup with it. For instance,

How to bind a simple popup displaying a message to marker?

To bind a simple popup displaying a message to a marker, follow the steps given below −. Step 1 − Create a Map object by passing a < div > element (String or object) and map options (optional). Step 2 − Create a Layer object by passing the URL of the desired tile. Step 3 − Add the layer object to the map using the addLayer () ...

What is leaflet JS?

Leaflet.js is an open-source library using which we can deploy simple, interactive, lightweight web maps. Leaflet JavaScript library allows you to use layers such as Tile layers, WMS, Markers, Popups, Vector layers (polylines, polygons, circles, etc.), Image overlays and GeoJSON.


2 Answers

Are you sure that you're reading the Leaflet reference documentation? It specifies that you can bind a popup with options by creating it and calling .bindPopup with it. For instance,

var popup = L.popup()     .setContent("I am a standalone popup.");  marker.bindPopup(popup).openPopup(); 
like image 115
tmcw Avatar answered Sep 18 '22 06:09

tmcw


You can pass an object of popup options as the second argument of bindPopup, like this:

marker.bindPopup("<strong>Hello world!</strong><br />I am a popup.", {maxWidth: 500}); 

I've tested this in Leaflet 1.4, and it also seems be available in earlier versions of bindPopup.

like image 42
Richard Garside Avatar answered Sep 18 '22 06:09

Richard Garside