-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
212 lines (159 loc) · 5.86 KB
/
main.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//This div will be used to show Google map
const mapArea = document.getElementById('map');
const $ = id => document.getElementById(id);
const actionBtn = document.getElementById('showMe');
const locationsAvailable = document.getElementById('locationList');
let Gmap, Gmarker;
const __KEY = "AIzaSyB2YeMKniOp8PvU2qPj99m7LTedKOEGYUM";
actionBtn.addEventListener('click', e => {
// hide the button
actionBtn.style.display = "none";
// call Materialize toast to update user
M.toast({ html: 'I am fetching your current location', classes: 'rounded' });
// get the user's position
getLocation();
});
getLocation = () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(displayLocation, showError, options)
} else {
M.toast({ html: 'Sorry, your browser does not support this feature... Please Update your Browser to enjoy it', classes: 'rounded' });
}
}
// displayLocation
displayLocation = (position) => {
const lat = position.coords.latitude;
const lng = position.coords.longitude;
const latlng = { lat, lng }
showMap(latlng, lat, lng);
createMarker(latlng);
mapArea.style.display = "block";
getGeolocation(lat, lng);
}
// Recreates the map
showMap = (latlng, lat, lng) => {
let mapOptions = {
center: latlng,
zoom: 17
};
Gmap = new google.maps.Map(mapArea, mapOptions);
Gmap.addListener('drag', function() {
Gmarker.setPosition(this.getCenter()); // set marker position to map center
});
Gmap.addListener('dragend', function() {
Gmarker.setPosition(this.getCenter()); // set marker position to map center
});
Gmap.addListener('idle', function() {
Gmarker.setPosition(this.getCenter()); // set marker position to map center
if (Gmarker.getPosition().lat() !== lat || Gmarker.getPosition().lng() !== lng) {
setTimeout(() => {
// console.log("I have to get new geocode here!")
updatePosition(this.getCenter().lat(), this.getCenter().lng()); // update position display
}, 2000);
}
});
}
// Creates marker on the screen
createMarker = (latlng) => {
let markerOptions = {
position: latlng,
map: Gmap,
animation: google.maps.Animation.BOUNCE,
clickable: true
// draggable: true
};
Gmarker = new google.maps.Marker(markerOptions);
}
// updatePosition on
updatePosition = (lat, lng) => {
getGeolocation(lat, lng);
}
// Displays the different error messages
showError = (error) => {
mapArea.style.display = "block"
switch (error.code) {
case error.PERMISSION_DENIED:
mapArea.innerHTML = "You denied the request for your location."
break;
case error.POSITION_UNAVAILABLE:
mapArea.innerHTML = "Your Location information is unavailable."
break;
case error.TIMEOUT:
mapArea.innerHTML = "Your request timed out. Please try again"
break;
case error.UNKNOWN_ERROR:
mapArea.innerHTML = "An unknown error occurred please try again after some time."
break;
}
}
const options = {
enableHighAccuracy: true
}
getGeolocation = (lat, lng) => {
const latlng = lat + "," + lng;
fetch(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${latlng}&key=${__KEY}`)
.then(res => res.json())
.then(data => populateCard(data.results));
}
function removeAddressCards() {
if (locationsAvailable.hasChildNodes()) {
while (locationsAvailable.firstChild) {
locationsAvailable.removeChild(locationsAvailable.firstChild);
}
}
}
populateCard = (geoResults) => {
// check if a the container has a child node to force re-render of dom
removeAddressCards();
geoResults.map(geoResult => {
// first create the input div container
const addressCard = document.createElement('div');
// then create the input and label elements
const input = document.createElement('input');
const label = document.createElement('label');
// then add materialize classes to the div and input
addressCard.classList.add("card");
input.classList.add("with-gap");
// add attributes to them
label.setAttribute("for", geoResult.place_id);
label.innerHTML = geoResult.formatted_address;
input.setAttribute("name", "address");
input.setAttribute("type", "radio");
input.setAttribute("value", geoResult.formatted_address);
input.setAttribute("id", geoResult.place_id);
// input.addEventListener('click', e => console.log(123));
input.addEventListener('click', () => inputClicked(geoResult));
// finalResult = input.value;
finalResult = geoResult.formatted_address;
addressCard.appendChild(input);
addressCard.appendChild(label);
// console.log(geoResult.formatted_address)
return (
locationsAvailable.appendChild(addressCard)
);
})
}
inputClicked = (result) => {
result.address_components.map(component => {
const types = component.types
if (types.includes('postal_code')) {
$('postal_code').value = component.long_name
}
if (types.includes('locality')) {
$('locality').value = component.long_name
}
if (types.includes('administrative_area_level_2')) {
$('city').value = component.long_name
}
if (types.includes('administrative_area_level_1')) {
$('state').value = component.long_name
}
if (types.includes('point_of_interest')) {
$('landmark').value = component.long_name
}
});
$('address').value = result.formatted_address;
// to avoid labels overlapping prefilled contents
M.updateTextFields();
removeAddressCards();
}