-
Notifications
You must be signed in to change notification settings - Fork 0
/
maps.js
151 lines (132 loc) · 4.25 KB
/
maps.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* Create google Maps Map instance.
* @param {number} lat
* @param {number} lng
* @return {Object}
*/
const createMap = ({ lat, lng }) => {
return new google.maps.Map(document.getElementById('map'), {
center: { lat, lng },
zoom: 18,
zoomControl: false,
streetViewControl: false,
});
};
const locataionicon = 'positive.png' // Icon for player
var myCoords; // Veriable for players position to be used in other functions
var myLat; // Variable to be used in myCoords
var myLng; // Variable to be used in myCoords
var quizCoords; // Variable to be set fot the coordinate of the question the player wants to answer
var distance; // Distance between player and the question
/**
* Create google maps Marker instance.
* @param {Object} map
* @param {Object} position
* @param {Object} icon
* @param {Object} animation
* @return {Object}
*/
const createMarker = ({ map, position, icon, animation }) => {
return new google.maps.Marker({ map, position, icon, animation });
};
/**
* Track the user location.
* @param {Object} onSuccess
* @param {Object} [onError]
* @return {number}
*/
const trackLocation = ({ onSuccess, onError = () => { } }) => {
if ('geolocation' in navigator === false) {
return onError(new Error('Geolocation is not supported by your browser.'));
}
return navigator.geolocation.watchPosition(onSuccess, onError, {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
});
};
/**
* Get position error message from the given error code.
* @param {number} code
* @return {String}
*/
const getPositionErrorMessage = code => {
switch (code) {
case 1:
return 'Permission denied.';
case 2:
return 'Position unavailable.';
case 3:
return 'Timeout reached.';
}
}
/**
* Initialize the application.
* Automatically called by the google maps API once it's loaded.
*/
function init() {
const initialPosition = { lat: 59.31, lng: 18.06 };
const map = createMap(initialPosition);
const myPosition = createMarker({
map,
position: initialPosition,
icon: locataionicon,
animation: google.maps.Animation.BOUNCE });
const $info = document.getElementById('info');
var infowindow = new google.maps.InfoWindow();
// Create markers and loop through the arrey
var marker, circleMarkers, i;
for (i = 0; i < myQuestions.length; i++) {
if(myQuestions[i].answered === false) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(myQuestions[i].center),
map: map,
label: i+1+""
});
// Create circles to show where you have to be within to answer question
circleMarkers = new google.maps.Circle({
center:new google.maps.LatLng(myQuestions[i].center),
radius:20,
strokeColor:"#B40404",
strokeOpacity:0.6,
strokeWeight:2,
fillColor:"#B40404",
fillOpacity:0.1
});
circleMarkers.setMap(map);
}
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
// On click, set coordinates for the question to the veriable quizCoords:
quizCoords = new google.maps.LatLng(myQuestions[i].center);
// On click, check distance between the question and the player:
distance = google.maps.geometry.spherical.computeDistanceBetween(myCoords, quizCoords);
console.log(distance); //bugtesting
// If distance is within 20 meters, show the question, hide the marker and set the question to answered:
if (distance<20) {
showSlide(i);
document.getElementById("quizdiv").style.display="block";
myQuestions[i].answered = true;
marker.setMap(null);
}
}
})(marker, i));
}
// Keep track of the players location and update players location when the players position is changed:
const watchId = trackLocation({
onSuccess: ({ coords: { latitude: lat, longitude: lng } }) => {
myPosition.setPosition({ lat, lng });
map.panTo({ lat, lng });
$info.textContent = `Lat: ${lat.toFixed(5)} Lng: ${lng.toFixed(5)}`;
$info.classList.remove('error');
myLat = lat;
myLng = lng;
myCoords = new google.maps.LatLng(myLat, myLng);
},
onError: err => {
console.log($info);
$info.textContent = `Error: ${err.message || getPositionErrorMessage(err.code)}`;
$info.classList.add('error');
}
});
}