Add draggable marker changes

This commit is contained in:
Eli Ribble 2026-01-08 23:38:38 +00:00
parent 0cd6697fd4
commit b4e91609d3
No known key found for this signature in database
3 changed files with 47 additions and 9 deletions

View file

@ -71,6 +71,7 @@ function displaySuggestions(suggestions) {
// Display the selected location details // Display the selected location details
displaySelectedLocation(suggestion); displaySelectedLocation(suggestion);
setMapMarker(suggestion.geometry.coordinates);
}); });
suggestionsContainer.appendChild(item); suggestionsContainer.appendChild(item);

View file

@ -2,11 +2,42 @@
<script src='https://api.mapbox.com/mapbox-gl-js/v3.17.0-beta.1/mapbox-gl.js'></script> <script src='https://api.mapbox.com/mapbox-gl-js/v3.17.0-beta.1/mapbox-gl.js'></script>
<link href='https://api.mapbox.com/mapbox-gl-js/v3.17.0-beta.1/mapbox-gl.css' rel='stylesheet' /> <link href='https://api.mapbox.com/mapbox-gl-js/v3.17.0-beta.1/mapbox-gl.css' rel='stylesheet' />
<script> <script>
var map = null;
var markers = [];
function setMapMarker(coords) {
console.log("Setting map marker", coords);
map.jumpTo({
center: coords,
zoom: 14,
});
markers.forEach((marker) => marker.remove());
addMarker(coords);
}
function addMarker(coords) {
const marker = new mapboxgl.Marker({
color: "#FF0000",
draggable: true
}).setLngLat(coords).addTo(map);
marker.on('dragend', onMapMarkerDragEnd(marker));
markers.push(marker);
}
function onMapMarkerDragEnd(marker) {
return function() {
const lngLat = marker.getLngLat();
displaySelectedCoordinates(lngLat);
}
}
function displaySelectedCoordinates(lngLat) {
const gpsDisplay = document.getElementById("gps-display");
gpsDisplay.classList.remove('d-none');
longitude.textContent = lngLat.lng;
latitude.textContent = lngLat.lat;
}
function onLoadMap() { function onLoadMap() {
const MAPBOX_ACCESS_TOKEN = '{{.MapboxToken}}'; const MAPBOX_ACCESS_TOKEN = '{{.MapboxToken}}';
console.log("Setting up the map..."); console.log("Setting up the map...");
mapboxgl.accessToken = MAPBOX_ACCESS_TOKEN; mapboxgl.accessToken = MAPBOX_ACCESS_TOKEN;
const map = new mapboxgl.Map({ map = new mapboxgl.Map({
container: "map", container: "map",
center: { center: {
lat: 36.2, lat: 36.2,
@ -42,14 +73,10 @@ function updateMapWithLocation(map) {
}, },
zoom: 14, zoom: 14,
}); });
const marker = new mapboxgl.Marker({ addMarker([
color: "#FF0000", position.coords.longitude,
draggable: true position.coords.latitude,
}).setLngLat({ ]);
lat: position.coords.latitude,
lng: position.coords.longitude,
}).addTo(map);
}, },
// on error // on error
function(error) { function(error) {

View file

@ -103,6 +103,16 @@
<div class="col-md-12"> <div class="col-md-12">
{{template "location-geocode"}} {{template "location-geocode"}}
</div> </div>
<div class="col-md-12 d-none" id="gps-display">
<div class="location-detail">
<div class="detail-label">Longitude</div>
<div id="longitude" class="detail-value">-</div>
</div>
<div class="location-detail">
<div class="detail-label">Latitude</div>
<div id="latitude" class="detail-value">-</div>
</div>
</div>
</div> </div>
<p class="small text-muted mb-2">You can also click on the map to mark the location precisely</p> <p class="small text-muted mb-2">You can also click on the map to mark the location precisely</p>