-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
141 lines (117 loc) · 3.92 KB
/
app.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
const getGoogleMapsApiKey = async () => {
const response = await fetch("/.env.json");
return (await response.json()).googleMapApiKey;
};
const getCoordinates = async () => {
const response = await fetch("/input/coordinates.json");
if (response.ok) {
return await response.json();
}
return [];
};
const loadGoogleMapsScript = async (googleMapApiKey, callbackFuncName) => {
const script = document.createElement("script");
script.src = `https://maps.googleapis.com/maps/api/js?key=${googleMapApiKey}&callback=${callbackFuncName}`;
script.id = "googleMaps";
document.body.appendChild(script);
};
const setupCoordinatesSelector = coordinates => {
const select = document.getElementById("select-coordinates");
select.innerHTML = "";
coordinates.forEach(area => {
var option = document.createElement("option");
option.value = area.name;
option.innerHTML = area.name;
select.appendChild(option);
});
select.addEventListener("change", event => {
loadMap(coordinates.find(x => x.name === event.target.value).area);
});
};
async function googleMapsScriptLoaded(coordinates) {
coordinates = coordinates || (await getCoordinates());
if (!coordinates || coordinates.length < 1) {
return;
}
setupCoordinatesSelector(coordinates);
loadMap(coordinates[0].area);
}
const getBoundsForCoordinates = coordinates => {
const bounds = new google.maps.LatLngBounds();
coordinates.forEach(coordinate => bounds.extend(coordinate));
return bounds;
};
const createMap = bounds => {
return new google.maps.Map(document.getElementById("map"), {
zoom: 5,
center: bounds.getCenter().toJSON(),
mapTypeId: google.maps.MapTypeId.ROADMAP,
streetViewControl: false,
mapTypeControl: false,
fullscreenControl: false
});
};
const createPolygon = coordinates => {
return new google.maps.Polygon({
paths: coordinates,
strokeColor: "#FF0000",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#FF0000",
fillOpacity: 0.35,
editable: true
});
};
const loadMap = async coordinates => {
logCoordinates(coordinates);
const bounds = getBoundsForCoordinates(coordinates);
const map = createMap(bounds);
const polygon = createPolygon(coordinates);
const path = polygon.getPath();
google.maps.event.addListener(path, "insert_at", polygonChanged);
google.maps.event.addListener(path, "remove_at", polygonChanged);
google.maps.event.addListener(path, "set_at", polygonChanged);
polygon.setMap(map);
map.fitBounds(bounds);
};
function polygonChanged() {
const coordinates = this.getArray().map(x => x.toJSON());
logCoordinates(coordinates);
}
const setupViewCoordinates = () => {
const button = document.getElementById("view-coordinates");
button.addEventListener("click", toggleViewCoordinatesOverlay);
};
const toggleViewCoordinatesOverlay = () => {
const log = document.getElementById("log");
const style = getComputedStyle(log);
log.style.display = style.display === "none" ? "block" : "none";
};
const logCoordinates = coordinates => {
var element = document.getElementById("log-coordinates");
element.textContent = JSON.stringify(coordinates, null, 2);
};
const setupUploadCoordinates = () => {
const upload = document.getElementById("upload");
upload.addEventListener("change", handleUploadedFile);
const uploadAction = document.getElementById("upload-action");
uploadAction.addEventListener("click", () => {
upload.click();
});
};
const handleUploadedFile = event => {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = function(evt) {
const data = JSON.parse(evt.target.result);
googleMapsScriptLoaded(data);
};
reader.readAsBinaryString(file);
};
(async () => {
const googleMapApiKey = await getGoogleMapsApiKey();
const callbackFuncName = googleMapsScriptLoaded.name;
await loadGoogleMapsScript(googleMapApiKey, callbackFuncName);
setupViewCoordinates();
setupUploadCoordinates();
})();