Leafletjs dynamically bound to visible markers/clusters - Javascript Leaflet

Javascript examples for Leaflet:Marker

Description

Leafletjs dynamically bound to visible markers/clusters

Demo Code

ResultView the demo in separate window

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

#map {/*from   w w w . j av  a2  s. com*/
   height: 500px;
}


      </style> 
      <script type="text/javascript">
    window.onload=function(){
var map = L.map("map");
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png").addTo(map);
map.setView([43.6532, -79.3832], 2);
var parentGroup = L.markerClusterGroup().addTo(map),
    consultingMarkers = L.featureGroup.subGroup(parentGroup).addTo(map),
    otherMarkers = L.featureGroup.subGroup(parentGroup).addTo(map);
L.marker([43.6425657, -79.38705569999999]).addTo(consultingMarkers);
L.marker([43.7164673, -79.3395846]).addTo(consultingMarkers);
L.marker([-41.3142772, 174.8135975], {/*icon: otherIcon*/}).addTo(otherMarkers);
var overlays = {
  'consulting': consultingMarkers,
  'other': otherMarkers
};
L.control.layers(null, overlays, {
  collapsed: false
}).addTo(map);
map.on('overlayadd overlayremove', function () {
  var bounds = parentGroup.getBounds();
  if (bounds.isValid()) {
     map.fitBounds(bounds);
  }
});
    }

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

Related Tutorials