Automatically open popup on layer add - Javascript Leaflet

Javascript examples for Leaflet:Layer

Description

Automatically open popup on layer add

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://unpkg.com/leaflet@1.0.0/dist/leaflet-src.js"></script> 
      <link rel="stylesheet" type="text/css" href="https://unpkg.com/leaflet@1.0.1/dist/leaflet.css"> 
      <script type="text/javascript" src="https://unpkg.com/leaflet@1.0.1/dist/leaflet-src.js"></script> 
      <style id="compiled-css" type="text/css">

#map {/*from w  w w . j a va2s .  co m*/
   height: 500px;
}


      </style> 
      <script type="text/javascript">
    window.onload=function(){
var map = L.map("map");
L.tileLayer("http://{s}.tile.osm.org/{z}/{x}/{y}.png").addTo(map);
map.setView([48.85, 2.35], 12);
var marker = L.marker([48.85, 2.35]).bindPopup("Hello World");
marker.on("add", function (event) {
   event.target.openPopup();
});
L.control.layers(null, {
   "My Marker": marker
}, {
   collapsed: false
}).addTo(map);
    }

      </script> 
   </head> 
   <body> 
      <div id="map"></div>  
   </body>
</html>

Related Tutorials