Skip to content

Instantly share code, notes, and snippets.

@1dolinski
Created September 24, 2020 19:33
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 1dolinski/469ef12a9c18b6bd31dd7ac9cc37c67e to your computer and use it in GitHub Desktop.
Save 1dolinski/469ef12a9c18b6bd31dd7ac9cc37c67e to your computer and use it in GitHub Desktop.
const googleGeoCode = (city, callback) => {
const url = `https://maps.googleapis.com/maps/api/geocode/json`;
const data = {
address: city,
key: googleClientAPIKey
}
$.ajax({ url: url, data: data, success: callback, dataType: 'json' });
}
const googleTimeZone = (location, callback) => {
var epochDate = new Date();
var timestamp = epochDate.getTime() / 1000
const url = `https://maps.googleapis.com/maps/api/timezone/json`;
const data = {
location: Object.values(location).join(','),
timestamp: timestamp,
key: googleClientAPIKey
}
$.ajax({ url: url, data: data, success: callback, dataType: 'json' });
}
if(window.google){
initMap();
} else{
$.ajax(ajaxAddress, {
crossDomain: true,
dataType: 'script'
});
}
const timeZoneSelector = () => {
timeZoneSearch.focusout(() => {
const val = timeZoneSearch.val();
const noTimeZone = (val === '' || val === undefined || val === null);
if (noTimeZone) { return };
googleGeoCode(val, geoCodeData => {
const geocodeLocation = geoCodeData.results[0].geometry.location;
googleTimeZone(geocodeLocation, timezoneData => {
timeZoneLink.text(timezoneData.timeZoneId); // set label
timeZoneValue.val(timezoneData.timeZoneId); // set form field
})
});
})
timeZoneSearch.keypress(function(event){
var keycode = (event.keyCode ? event.keyCode : event.which);
if (keycode == '13') {
$(this).blur();
event.preventDefault();
event.stopPropagation();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment