Skip to content

Instantly share code, notes, and snippets.

@akmur
Created March 14, 2017 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akmur/3a74b84c07fa66f7895d2e2585269961 to your computer and use it in GitHub Desktop.
Save akmur/3a74b84c07fa66f7895d2e2585269961 to your computer and use it in GitHub Desktop.
Geolocation Module
/* global document google */
/**
* This file contains code for the maps found in the website.
* It takes care of converting an address to a location by using google's geocode code.
*
* @summary Map Loader
*/
const mapLoader = (function(){
const mapsOnPage = document.querySelectorAll('.js-map');
function geocodeAddress(geocoder, resultsMap, address){
const defaultPosition = {lat: 45.4654, lng: 9.1859};
geocoder.geocode({address}, (results, status) => {
if (status === 'OK'){
resultsMap.setCenter(results[0].geometry.location);
const marker = new google.maps.Marker({
map: resultsMap,
position: results[0].geometry.location,
animation: google.maps.Animation.DROP
});
} else {
resultsMap.setCenter(milanoPosition);
const marker = new google.maps.Marker({
map: resultsMap,
position: defaultPosition,
animation: google.maps.Animation.DROP
});
}
});
}
function generateMap(mapContainer){
const address = mapContainer.dataset.address;
const googleMap = new google.maps.Map(mapContainer, {
zoom: 15,
center: {lat: -34.397, lng: 150.644}
});
const geocoder = new google.maps.Geocoder();
geocodeAddress(geocoder, googleMap, address);
}
function init(){
for (const mapContainer of mapsOnPage){
generateMap(mapContainer);
}
}
return {
init
};
})();
module.exports = mapLoader;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment