-
Notifications
You must be signed in to change notification settings - Fork 4
/
select-location.js
59 lines (50 loc) · 2.09 KB
/
select-location.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
Template.devSelectLocation.rendered = function () {
var template = this;
var input = template.find('.select-location input');
// Create autocomplete
autocomplete = null;
autocomplete && google.maps.event.clearListeners(autocomplete);
autocomplete = new google.maps.places.Autocomplete(input);
google.maps.event.addListener(
autocomplete, 'place_changed', function () {
var place = autocomplete.getPlace();
var timeStamp, timeZoneApiRequestUrl;
var timeZoneApiErrorCallback;
if (place.utc_offset !== void 0) {
Session.set("selectedLocationUTCOffset", place.utc_offset / 60);
}
else {
// An address not a generic term like "pizza" was specified so the utc_offset was not provided.
// Google Time Zone API to the rescue.
timeZoneApiErrorCallback = function() {
Alerts.throw({
message: "Hmm, something went wrong trying to figure out the time zone for your game location. Retype the location in the 'Add Venue' field to try again.",
type: "danger", where: "editGame", autoremove: 3000
});
};
timeStamp = (Date.now() || new Date().getTime())/1000;
timeZoneApiRequestUrl = "https://maps.googleapis.com/maps/api/timezone/json?location=" +
place.geometry.location.lat() + "," + place.geometry.location.lng() +
"×tamp=" + timeStamp + "&key=AIzaSyD-RvRraZIUUrcrdWtYZx-Ec0vzUGyMFXg";
$.ajax({
url: timeZoneApiRequestUrl
}).done(function(data) {
try {
var utcOffset = (data.rawOffset + data.dstOffset || 0) / 3600;
Session.set("selectedLocationUTCOffset", utcOffset);
}
catch(e) {
timeZoneApiErrorCallback(e.message);
}
}).error(function(error) {
timeZoneApiErrorCallback(error);
});
}
if (place.geometry) {
Session.set("selectedLocationPoint",
geoUtils.toGeoJSONPoint(place.geometry.location));
Session.set("selectedLocationName", place.name + ", " + place.vicinity);
}
}
);
};