Created
March 14, 2017 16:50
-
-
Save akmur/3a74b84c07fa66f7895d2e2585269961 to your computer and use it in GitHub Desktop.
Geolocation Module
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
/* 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