Created
August 8, 2018 09:33
-
-
Save jackabox/8ad479503813bcc4f2a553283202cbd6 to your computer and use it in GitHub Desktop.
Pure JS map foreach with html data-attr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="map-holder" data-lat="{{ office.map.lat }}" data-lng="{{ office.map.lng }}" data-zoom="{{ office.map.zoom }}"> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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