Skip to content

Instantly share code, notes, and snippets.

@petersmythe
Last active September 5, 2018 11:39
Show Gist options
  • Save petersmythe/9b707dfb6658d2d58fa2c90c5ee5b872 to your computer and use it in GitHub Desktop.
Save petersmythe/9b707dfb6658d2d58fa2c90c5ee5b872 to your computer and use it in GitHub Desktop.
<script>
//Map the offices array to an array Leaflet Markers, and add them to the map. Bind a function to the popup event.
const officeMarkers = offices.map((element) => {
const marker = L.marker(L.latLng([element.latitude, element.longitude]));
marker.bindPopup(officeInfo(element))
.addTo(map);
return marker;
});
//Add the array of Leaflet Markers to a Leaflet Feature group. Fit the bounds of the map the bounds of the Feature group.
const group = new L.featureGroup(officeMarkers);
map.fitBounds(group.getBounds());
//Function to return the HTML string for display within a Leaflet Popup.
function officeInfo(office) {
return `<div class='at-leaflet-popup'>
<div class='at-leaflet-popup__content'>
<dl class='at-leaflet-popup__info'>
<dt>${office.name}</dt>
</dl>
</div>
</div>`;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment