Skip to content

Instantly share code, notes, and snippets.

@jackabox
Created August 8, 2018 09:33
Show Gist options
  • Save jackabox/8ad479503813bcc4f2a553283202cbd6 to your computer and use it in GitHub Desktop.
Save jackabox/8ad479503813bcc4f2a553283202cbd6 to your computer and use it in GitHub Desktop.
Pure JS map foreach with html data-attr
<div class="map-holder" data-lat="{{ office.map.lat }}" data-lng="{{ office.map.lng }}" data-zoom="{{ office.map.zoom }}">
function initMaps() {
let mapHolders = document.getElementsByClassName('map-holder');
Array.prototype.filter.call(mapHolders, map => {
let lat = Number(map.dataset.lat);
let lng = Number(map.dataset.lng);
let zoom = Number(map.dataset.zoom);
// Display the map
let instance = new google.maps.Map(map, {
center: {
lat: lat,
lng: lng
},
zoom: zoom
});
// Display the marker
var marker = new google.maps.Marker({
position: {
lat: lat,
lng: lng
},
map: instance
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment