-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
102 lines (87 loc) · 2.04 KB
/
app.ts
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
const mapContainerNode = document.getElementById("map");
let map: google.maps.Map;
interface LocationType {
id: string;
name: string;
locations: string;
}
interface Location {
id: string;
name: string;
location: {
latitude: number;
longitude: number;
};
}
interface Position {
coords: {
latitude: number;
longitude: number;
}
}
function initMap(position: GeolocationPosition) {
map = new google.maps.Map(mapContainerNode, {
center: {
lat: position.coords.latitude,
lng: position.coords.longitude
},
zoom: 15,
});
}
function getMyPosition(): Promise<GeolocationPosition> {
return new Promise(function (resolve, reject) {
navigator.geolocation.getCurrentPosition(
function (position) {
resolve(position);
},
function (error) {
reject(error);
}
);
});
}
function addMarkers(locations: Location[]) {
for (let location of locations) {
new google.maps.Marker({
position: {
lat: location.location.latitude,
lng: location.location.longitude
},
map: map,
label: location.name
})
}
}
function getLocationTypes(): Promise<LocationType[]> {
return fetch('https://www.vullum.io/academy/oslo.json')
.then((response) => response.json());
}
function getLocations(url: string): Promise<Location[]> {
return fetch(url)
.then((response) => response.json())
}
function getFlattenedLocations() {
return getLocationTypes()
.then((locationTypes) => {
let locationUrls = locationTypes
.map((locationType) => locationType.locations);
let locationPromises = locationUrls
.map((url) => getLocations(url));
let locations = Promise.all(locationPromises)
.then((locationCollection) => {
return locationCollection.flat();
})
return locations;
})
}
async function main() {
try {
const myPosition = await getMyPosition();
initMap(myPosition);
const locations = await getFlattenedLocations();
addMarkers(locations);
} catch (error) {
console.log(error);
}
}
main();