Skip to content

Commit 3e1083a

Browse files
committed
add geo location service to view current position
1 parent 5e314dd commit 3e1083a

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

src/index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ <h2><a href="/" class="text-xl md:text-2xl text-gray-700 hover:text-blue-700 foc
5858
</div>
5959

6060
<div class="mt-3 md:mt-6 py-2 px-3" id="about">
61+
<form id="geoLocation" enctype="application/x-www-form-urlencoded">
62+
<div class="mx-3 mb-8">
63+
<label class="relative inline-flex items-center cursor-pointer">
64+
<input type="checkbox" name="myLocation" value="true" class="sr-only peer">
65+
<div
66+
class="w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-blue-300 dark:peer-focus:ring-blue-800 rounded-full peer dark:bg-gray-700 peer-checked:after:translate-x-full rtl:peer-checked:after:-translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:start-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-gray-600 peer-checked:bg-blue-600">
67+
</div>
68+
<span class="ms-3 text-sm font-medium text-gray-900 dark:text-gray-300">Meinen Standort verwenden</span>
69+
</label>
70+
</div>
71+
</form>
72+
6173
<h3 class="font-bold text-xl md:text-2xl mb-1">Wie funktioniert die Denkmalkarte?</h3>
6274
<p class="font-normal leading-normal text-md lg:text-lg mb-3 md:mb-6">Klicken Sie auf einen Marker, um Details
6375
zu denkmalgeschützten Anlagen anzuzeigen. Bewegen Sie die Karte, um weitere Orte zu erkunden – die Daten

src/main.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,19 @@ async function fetchMonumentPointsByBounds() {
163163
ymax: bounds.getNorth(),
164164
}
165165

166-
const url = `${process.env.PARCEL_BASE_API_URL}/monument/v1/geometries?xmin=${bbox.xmin}&ymin=${bbox.ymin}&xmax=${bbox.xmax}&ymax=${bbox.ymax}`
166+
const url = `${process.env.PARCEL_BASE_API_URL}/monument/v1/bounds?xmin=${bbox.xmin}&ymin=${bbox.ymin}&xmax=${bbox.xmax}&ymax=${bbox.ymax}`
167+
const data = await fetchJsonData(url)
168+
addMonumentsToMap(data, addMonumentsByBounds, zoomLevelInitial)
169+
170+
if (previousSelectedMarker) {
171+
const previousMarkerId = previousSelectedMarker.feature.id
172+
const newSelectedMarker = findMarkerById(previousMarkerId)
173+
if (newSelectedMarker) setSelectedMarker(newSelectedMarker)
174+
}
175+
}
176+
177+
async function fetchMonumentPointsByPosition(lat, lng) {
178+
const url = `${process.env.PARCEL_BASE_API_URL}/monument/v1/radius?lat=${lat}&lng=${lng}`
167179
const data = await fetchJsonData(url)
168180
addMonumentsToMap(data, addMonumentsByBounds, zoomLevelInitial)
169181

@@ -175,7 +187,7 @@ async function fetchMonumentPointsByBounds() {
175187
}
176188

177189
async function fetchMonumentDetailBySlug(slug) {
178-
const url = `${process.env.PARCEL_BASE_API_URL}/monument/v1/detail?slug=${slug}`
190+
const url = `${process.env.PARCEL_BASE_API_URL}/monument/v1/details?slug=${slug}`
179191
const data = await fetchJsonData(url)
180192

181193
if (!data || !data[0]) return
@@ -223,7 +235,6 @@ async function fetchMonumentDetailById(id) {
223235
renderMonumentMeta(data[0])
224236
}
225237

226-
// UI-related functions
227238
function renderMonumentMeta(data) {
228239
const { slug, street, housenumber, postcode, city, last_update, monument_type, description, monument_function, object_number, monument_scope } = data
229240

@@ -294,6 +305,24 @@ window.onload = async () => {
294305
document.querySelector('#sidebar').classList.toggle('translate-y-full')
295306
})
296307

308+
document.getElementById('geoLocation').addEventListener('change', function (event) {
309+
if (event.target.name === 'myLocation' && event.target.checked) {
310+
if (navigator.geolocation) {
311+
navigator.geolocation.getCurrentPosition(
312+
(position) => {
313+
fetchMonumentPointsByPosition(position.coords.latitude, position.coords.longitude)
314+
map.setView([position.coords.latitude, position.coords.longitude], 16)
315+
},
316+
(error) => {
317+
console.error('Error obtaining geolocation:', error.message);
318+
}
319+
);
320+
} else {
321+
console.error('Geolocation is not supported by this browser.');
322+
}
323+
}
324+
})
325+
297326
const path = decodeURIComponent(window.location.pathname)
298327
const screen = path === '/' ? 'home' : path.slice(1)
299328
if (!history.state) history.replaceState({ screen }, '', path)

0 commit comments

Comments
 (0)