-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
249 lines (217 loc) · 9.06 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
<!DOCTYPE html>
<html>
<head>
<title>Animal Shelter Finder</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css" />
<link rel="stylesheet" href="https://unpkg.com/leaflet-control-geocoder/dist/Control.Geocoder.css" />
<link rel="stylesheet" href="assets/styles.css" />
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
<script src="https://unpkg.com/leaflet-control-geocoder/dist/Control.Geocoder.js"></script>
<script src="https://unpkg.com/jquery/dist/jquery.min.js"></script>
<style>
/* Style the modal */
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0, 0, 0);
background-color: rgba(0, 0, 0, 0.4);
}
/* Modal Content */
.modal-content {
background-color: #fefefe;
margin: 15% auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
max-width: 500px;
border-radius: 10px;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
text-align: center;
}
/* Modal Header */
.modal-content h2 {
margin-top: 0;
color: #333;
}
/* Modal Body */
.modal-content p {
color: #666;
}
/* Modal Buttons */
.modal-content button {
background-color: #007bff;
color: white;
padding: 10px 20px;
margin: 10px;
border: none;
border-radius: 5px;
cursor: pointer;
}
.modal-content button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<h1 class="title">I want a cat!</h1>
<div id="map"></div>
<!-- Consent Modal -->
<div id="consentModal" class="modal">
<div class="modal-content">
<h2>Consent Required</h2>
<p>We need your consent to load the map and identify your location. Do you agree?</p>
<p>For more information, please read our <a href="gdpr.html" target="_blank">GDPR policy</a>.</p>
<button id="consentYes">Yes</button>
<button id="consentNo">No</button>
</div>
</div>
<script>
// Define constants for the map and geolocation services
const MAP_ID = 'map';
const GEOLOCATION_SUPPORTED = navigator.geolocation ? true : false;
const IP_GEOLOCATION_URL = "https://ipapi.co/json/";
const OVERPASS_API_URL = "https://overpass-api.de/api/interpreter";
// Initialize the map
let map;
// Check for consent cookie
const consentCookie = getCookie('userConsent');
if (consentCookie === 'yes') {
initializeMapWithConsent();
} else {
// Show consent modal
document.getElementById('consentModal').style.display = 'block';
}
document.getElementById('consentYes').addEventListener('click', function() {
document.getElementById('consentModal').style.display = 'none';
setCookie('userConsent', 'yes', 365);
initializeMapWithConsent();
});
document.getElementById('consentNo').addEventListener('click', function() {
document.getElementById('consentModal').style.display = 'none';
window.location.href = 'error.html';
});
function initializeMapWithConsent() {
// First, find location from IP
findLocationFromIP().then(() => {
// Then, if geolocation is supported, ask the user for their location
if (GEOLOCATION_SUPPORTED) {
navigator.geolocation.getCurrentPosition(updateMap, handleGeolocationError);
}
else {
handleIPGeolocationError();
}
});
}
function findLocationFromIP() {
return new Promise((resolve, reject) => {
$.get(IP_GEOLOCATION_URL, (position) => {
initializeMap(position);
resolve();
}).fail(() => {
handleIPGeolocationError();
resolve();
});
});
}
function handleIPGeolocationError() {
console.error("Failed to retrieve location from IP address.");
const defaultLocation = [48.5734053, 13.4503279]; // Fallback to Passau in Bayern
initializeMap({ coords: { latitude: defaultLocation[0], longitude: defaultLocation[1] } });
}
function updateMap(position) {
let center = normalizeCoordinates(position);
map.setView(center, 13);
map.panTo(center);
findAnimalShelters(center);
}
function handleGeolocationError(error) {
console.error(error);
findLocationFromIP();
}
function normalizeCoordinates(position) {
let center;
if (position.coords) {
// Geolocation API
center = [position.coords.latitude, position.coords.longitude];
} else {
// IP geolocation service
center = [position.latitude, position.longitude];
}
return center;
}
function linkify(text) {
const urlPattern = /\b(?:https?:\/\/)?(?:(?:[a-z\d](?:[a-z\d-]*[a-z\d])?\.)+[a-z]{2,}|localhost|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?::\d+)?(?:\/\S*)?\b/gi;
return text.replace(urlPattern, function (url) {
return `<a href="${url.startsWith('http') ? url : `https://${url}`}" target="_blank">${url}</a>`;
});
}
function initializeMap(position) {
let center = normalizeCoordinates(position);
map = L.map(MAP_ID).setView(center, 13);
map.panTo(center);
// Add OpenStreetMap tile layer to the map
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
// Create a geocoder control and add it to the map
L.Control.geocoder({
defaultMarkGeocode: false,
collapsed: false,
placeholder: 'find the closest animal shelter',
})
.on('markgeocode', function (e) {
const center = e.geocode.center;
map.setView(center, 13);
findAnimalShelters(center);
})
.addTo(map);
findAnimalShelters(center);
}
function findAnimalShelters(center) {
const latitude = center.lat || center[0];
const longitude = center.lng || center[1];
const query = `[out:json][timeout:25];(node["amenity"="animal_shelter"](around:50000,${latitude},${longitude}););out body;`;
$.get(OVERPASS_API_URL, { data: query }, handleOverpassResponse)
.fail(handleOverpassError);
}
function handleOverpassResponse(data) {
data.elements.forEach(element => {
const name = element.tags.name || 'Unknown Animal Shelter';
const info = Object.keys(element.tags)
.map(key => `<div><tt>${key}: ${linkify(element.tags[key])}</tt></div>`)
.join('\n');
const marker = L.marker([element.lat, element.lon]).addTo(map)
.bindPopup(`<b>${name}</b><div>${info}</div><button class="route-button" onclick="window.open('https://www.google.com/maps/dir/?api=1&destination=${element.lat},${element.lon}', '_blank')"><img src="https://cdn-icons-png.flaticon.com/512/1483/1483336.png" alt="Navigation Logo" class="route-icon"> Route to ${name}</button>`) // attribution to https://www.flaticon.com/ for providing this icon free to use
.openPopup();
map.panTo([element.lat, element.lon]);
});
}
function handleOverpassError() {
console.error("Failed to retrieve data from the Overpass API.");
}
function setCookie(name, value, days) {
const d = new Date();
d.setTime(d.getTime() + (days * 24 * 60 * 60 * 1000));
const expires = "expires=" + d.toUTCString();
document.cookie = name + "=" + value + ";" + expires + ";path=/";
}
function getCookie(name) {
const nameEQ = name + "=";
const ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
</script>
</body>
</html>