-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
250 lines (230 loc) · 12.6 KB
/
index.html
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Minimal Maps</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;600;800&display=swap" rel="stylesheet">
<link rel="stylesheet" href="static/custom.css">
</head>
<body>
<main>
<div class="container">
<h1>Minimal Maps</h1>
<form>
<div class="section">
<div class="section">
<label>
<input type="text" id="location" required placeholder="🔍 Type a location"/>
</label>
<input type="hidden" id="palette" name="palette" value="">
</div>
<div class="section">
<h4>Choose a palette</h4>
<div class="palettes_grid">
<div class="palette">
<div class="color_sector" style="background-color: #2d2d2d;"></div>
<div class="color_sector" style="background-color: #2d3535"></div>
<div class="color_sector" style="background-color: #405373"></div>
<div class="color_sector" style="background-color: #85ffdd"></div>
</div>
<div class="palette">
<div class="color_sector" style="background-color: #314e88;"></div>
<div class="color_sector" style="background-color: #cad6e2"></div>
<div class="color_sector" style="background-color: #4b95d2"></div>
<div class="color_sector" style="background-color: #3789d1"></div>
</div>
<div class="palette">
<div class="color_sector" style="background-color: #7ec583;"></div>
<div class="color_sector" style="background-color: #3ead82"></div>
<div class="color_sector" style="background-color: #22b284"></div>
<div class="color_sector" style="background-color: #f8f37e"></div>
</div>
<div class="palette">
<div class="color_sector" style="background-color: #7c2050;"></div>
<div class="color_sector" style="background-color: #4e1538"></div>
<div class="color_sector" style="background-color: #a13158"></div>
<div class="color_sector" style="background-color: #de568c"></div>
</div>
<div class="palette">
<div class="color_sector" style="background-color: #181818;"></div>
<div class="color_sector" style="background-color: #000000"></div>
<div class="color_sector" style="background-color: #212121"></div>
<div class="color_sector" style="background-color: #3c3c3c"></div>
</div>
<div class="palette">
<div class="color_sector" style="background-color: #162852;"></div>
<div class="color_sector" style="background-color: #2f408e"></div>
<div class="color_sector" style="background-color: #142d5f"></div>
<div class="color_sector" style="background-color: #5dd6b9"></div>
</div>
<div class="palette">
<div class="color_sector" style="background-color: #5d6883;"></div>
<div class="color_sector" style="background-color: #2b313a"></div>
<div class="color_sector" style="background-color: #40495c"></div>
<div class="color_sector" style="background-color: #73bcbb"></div>
</div>
<div id="open_custom_palette">
<img src="static/palette.svg" alt="custom palette"/>
</div>
<div id="open_settings">
<img src="static/settings.svg" alt="settings"/>
</div>
</div>
</div>
<div class="section">
<input type="submit" name="create_map" value="Create map">
</div>
<dialog id="settings_dialog">
<h2>Settings</h2>
<div class="dialog_content">
<label for="zoom_level">Aproximate zoom level:</label>
<select id="zoom_level" name="zoom_level">
<option value="5">Continent</option>
<option value="10">City</option>
<option value="14" selected="selected">Streets</option>
<option value="18">Buildings</option>
</select>
<button type="button" id="close_settings" class="pretty_button">Done</button>
</div>
</dialog>
</div>
</form>
<div id="error_div" class="section"></div>
<div id="map_div" class="section">
<img id='map' src="" alt="Your map">
</div>
<a id="download" target="_blank" href="">
<button type="button" class="pretty_button">Download</button>
</a>
<dialog id="custom_pallete_dialog">
<h2>Choose your own colors</h2>
<div class="dialog_content">
<div id="custom_palette">
<label for="land_color">Land</label>
<input type="color" id="land_color" value="#15464c" style="background-color: white">
<label for="water_color">Water</label>
<input type="color" id="water_color" value="#36ce7e" style="background-color: white">
<label for="city_color">City</label>
<input type="color" id="city_color" value="#474747" style="background-color: white">
<label for="roads_color">Roads</label>
<input type="color" id="roads_color" value="#212121" style="background-color: white">
</div>
<button type="button" id="close_custom_palette" class="pretty_button">Done</button>
</div>
</dialog>
</div>
</main>
<footer>
<p>© Laureano Ruiz · <a href="https://www.linkedin.com/in/laureanorp/">LinkedIn</a> · <a
href="https://github.com/laureanorp/minimal_maps">GitHub repo</a></p>
</footer>
<script>
// Set your own Google Maps Static API key here if you want to run the app locally
const gmaps_api_key = 'AIzaSyBPJmZeJP7sQ13wiMaBKmCtwrbqwl8BvAA';
// Init empty color palette
let colors = [];
const rgba2hex = (rgba) =>
`${rgba.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+\.{0,1}\d*))?\)$/).slice(1).map((n, i) => (i === 3 ? Math.round(parseFloat(n) * 255) : parseFloat(n)).toString(16).padStart(2, '0').replace('NaN', '')).join('')}`
// Init custom palette button to apply effects
const open_custom_palette = document.querySelector('#open_custom_palette')
// When we click on palette div, add the "selected" class
const built_in_palettes = document.querySelectorAll(".palette");
for (const palette of built_in_palettes) {
palette.addEventListener('click', function handleClick() {
// toggle selected class on the clicked palette, remove from the rest
for (const palette of built_in_palettes) {
palette.classList.remove('selected');
}
open_custom_palette.classList.remove('selected');
this.classList.add('selected');
// reset list of selected colors
colors = [];
// save to the list all the background colors of the selected palette
const color_sectors = this.querySelectorAll('.color_sector');
for (const color_sector of color_sectors) {
colors.push(rgba2hex(color_sector.style.backgroundColor));
}
// save the list to the hidden field that we will send to the backend
const palette_field = document.getElementById("palette")
palette_field.value = colors.join('-');
});
}
const custom_pallete_dialog = document.querySelector('#custom_pallete_dialog');
const close_custom_palette = document.querySelector('#close_custom_palette');
open_custom_palette.addEventListener('click', () => custom_pallete_dialog.showModal());
close_custom_palette.addEventListener('click', function handleClick() {
for (const palette of built_in_palettes) {
palette.classList.remove('selected');
}
// reset list of selected colors
colors = [];
// the user is setting a custom palette, so we get the colors from the input fields
const custom_colors = document.querySelectorAll('input[type="color"]');
for (const custom_color of custom_colors) {
colors.push(custom_color.value.replace('#', ''));
}
open_custom_palette.classList.add('selected');
// save the list to the hidden field that we will send to the backend
const palette_field = document.getElementById("palette");
palette_field.value = colors.join('-');
custom_pallete_dialog.close();
})
// Settings dialog
const open_settings = document.querySelector('#open_settings')
const close_settings = document.querySelector('#close_settings');
const settings_dialog = document.querySelector('#settings_dialog');
open_settings.addEventListener('click', () => settings_dialog.showModal());
close_settings.addEventListener('click', () => settings_dialog.close());
// when the user clicks the create map button
const form = document.querySelector('form');
form.addEventListener('submit', (e) => {
// don't "really" submit the form
e.preventDefault();
// if the user didn't select a palette, show an error
const palette_field = document.querySelector("#palette");
const error_div = document.querySelector("#error_div");
if (palette_field.value === '') {
error_div.style.display = 'revert';
error_div.innerHTML = '<p>Please select a palette to continue</p>';
return;
} else { // if the user selected a palette, hide the error
error_div.innerHTML = '';
error_div.style.display = 'none';
}
// Construct the map URL with the selected options
let url = new URL('https://maps.googleapis.com/maps/api/staticmap');
url.searchParams.append('size', '1280x1280');
url.searchParams.append('zoom', document.querySelector("#zoom_level").value);
url.searchParams.append('center', document.querySelector("#location").value);
url.searchParams.append('style',
`feature:all|element:all|color:0x${colors[0]}`); // main color (land)
url.searchParams.append('style',
`feature:road|element:geometry|color:0x${colors[3]}`); // roads color
url.searchParams.append('style',
'feature:road|element:geometry|visibility:simplified');
url.searchParams.append('style',
`feature:water|element:geometry|color:0x${colors[1]}`); // water color
url.searchParams.append('style',
`feature:landscape.man_made|geometry|color:0x${colors[2]}`); // cities color
url.searchParams.append('style',
'feature:all|element:labels|visibility:off');
url.searchParams.append('style',
'feature:transit|element:all|visibility:off');
url.searchParams.append('key', gmaps_api_key);
url.searchParams.append('scale', '2');
// Set source of the rendered map image to the url generated by GMaps API
document.querySelector('#map').src = url;
const download = document.querySelector('#download')
download.href = url
// Turn map and download button visible
download.style.display = 'revert';
document.querySelector('#map_div').style.display = 'flex';
})
</script>
</body>
</html>