-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
49 lines (42 loc) · 1.7 KB
/
script.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
// Create the script tag, set the appropriate attributes
var script = document.createElement("script");
script.src = "https://maps.googleapis.com/maps/api/js?key=AIzaSyAN1HuZR2h3OStGDn61coErkA7k4QHfrbI&callback=initMap";
script.defer = true;
var map, infoWindow;
// Attach your callback function to the `window` object
function initMap() {
map = new google.maps.Map(document.getElementById("map"), {
center: {lat: 47.609393, lng: -122.342764},
zoom: 6,
options: {
gestureHandling: "greedy"
}
});
infoWindow = new google.maps.InfoWindow;
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent("Location found.");
infoWindow.open(map);
map.setCenter(pos);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
"Error: The Geolocation service failed." :
"Error: Your browser doesn\'t support geolocation.");
infoWindow.open(map);
}
document.head.appendChild(script);