forked from southojere/MittensMaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (67 loc) · 2.19 KB
/
index.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
var placingSpotting = false;
function initMap() {
console.log("Loading map...");
//Center in location of wellington
var wellington = { lat: -41.2871926, lng: 174.7762 };
var map = new google.maps.Map(
document.getElementById('map'), { zoom: 15, center: wellington }
);
console.log("Loaded map");
//loading previously added spottings
loadSpottings(map);
//add new markers
map.addListener('click', function (e) {
if (placingSpotting) {
placeMarkerAndPanTo(e.latLng, map);
}
})
}
function addSighting() {
placingSpotting = !placingSpotting;
if (placingSpotting) alert("You can now add sightings");
else alert("adding sightings now turned off")
// bootbox.alert({
// message: "You are now adding sightings",
// className: 'bb-alternate-modal'
// });
}
leaveMessage = async () => {
var message = prompt("Care to leave a message :3");//TODO add to firebase
const response = await fetch("https://gitsearch-ff7bc.firebaseio.com/messages.json", {
method: "POST",
body: JSON.stringify(message)
}).catch(err => console.log(err));
}
function placeMarkerAndPanTo(latLng, map) {
addMarkerToCollection(latLng);
var marker = new google.maps.Marker({
position: latLng,
map: map,
draggable: true
});
map.panTo(latLng);
}
//FIREBASE STUFF
addMarkerToCollection = async (latLng) => {
console.log("adding to firebase...")
const response = await fetch("https://gitsearch-ff7bc.firebaseio.com/spottings.json", {
method: "POST",
body: JSON.stringify(latLng)
}).catch(err => console.log(err));
console.log("added")
}
loadSpottings = async (map) => {
const fireBaseSpottings = await fetch('https://gitsearch-ff7bc.firebaseio.com/spottings.json?print=pretty').then(res => res.json())
const keys = await Object.keys(fireBaseSpottings);
const spottings = await keys.map(key => ({
lat: fireBaseSpottings[key].lat,
lng: fireBaseSpottings[key].lng
}));
//now load into map
await spottings.forEach((e) => {
new google.maps.Marker({
position: e,
map: map,
});
})
}