RoyAalekh commited on
Commit
e60c2f9
·
1 Parent(s): bd7e2c8

Fix map pin location selection issue

Browse files

Problem: After dropping a pin on the map, clicking 'Use This Location'
showed 'No location selected' error message.

Root Cause: The onMapClick method was calling clearTempMarker() before
setting the new location, which reset selectedLocation and isLocationSelected
to null/false.

Solution: Reordered operations in onMapClick to only remove the old marker
without clearing the location state, then set the new location and create
the new marker.

This ensures the location state remains valid when the user clicks
'Use This Location' button.

Files changed (1) hide show
  1. static/map.js +30 -18
static/map.js CHANGED
@@ -254,12 +254,16 @@ class TreeTrackMap {
254
  onMapClick(e) {
255
  console.log('Map clicked at:', e.latlng);
256
 
 
 
 
 
 
 
 
257
  this.selectedLocation = e.latlng;
258
  this.isLocationSelected = true;
259
 
260
- // Remove existing temp marker
261
- this.clearTempMarker();
262
-
263
  // Create beautiful tree-shaped temp marker
264
  const tempIcon = L.divIcon({
265
  html: `
@@ -322,25 +326,33 @@ class TreeTrackMap {
322
  }
323
 
324
  useSelectedLocation() {
325
- if (!this.selectedLocation) {
326
- this.showMessage('No location selected', 'error');
327
  return;
328
  }
329
 
330
- // Store location for the form page
331
- localStorage.setItem('selectedLocation', JSON.stringify({
332
- lat: this.selectedLocation.lat,
333
- lng: this.selectedLocation.lng
334
- }));
 
 
 
335
 
336
- // Clear the message and redirect
337
- const messageElement = document.getElementById('message');
338
- if(messageElement) messageElement.classList.remove('show');
339
- this.showMessage('Location saved! Redirecting to form...', 'success');
340
-
341
- setTimeout(() => {
342
- window.location.href = '/';
343
- }, 1000);
 
 
 
 
 
344
  }
345
 
346
  cancelLocationSelection() {
 
254
  onMapClick(e) {
255
  console.log('Map clicked at:', e.latlng);
256
 
257
+ // Remove existing temp marker first (before setting new location)
258
+ if (this.tempMarker) {
259
+ this.map.removeLayer(this.tempMarker);
260
+ this.tempMarker = null;
261
+ }
262
+
263
+ // Now set the new location
264
  this.selectedLocation = e.latlng;
265
  this.isLocationSelected = true;
266
 
 
 
 
267
  // Create beautiful tree-shaped temp marker
268
  const tempIcon = L.divIcon({
269
  html: `
 
326
  }
327
 
328
  useSelectedLocation() {
329
+ if (!this.selectedLocation || !this.isLocationSelected) {
330
+ this.showMessage('No location selected. Please click on the map to drop a pin first.', 'error');
331
  return;
332
  }
333
 
334
+ try {
335
+ // Store location for the form page
336
+ const locationData = {
337
+ lat: this.selectedLocation.lat,
338
+ lng: this.selectedLocation.lng
339
+ };
340
+
341
+ localStorage.setItem('selectedLocation', JSON.stringify(locationData));
342
 
343
+ // Clear any previous messages and show success
344
+ const messageElement = document.getElementById('message');
345
+ if(messageElement) messageElement.classList.remove('show');
346
+ this.showMessage('Location saved! Redirecting to form...', 'success');
347
+
348
+ // Redirect after a short delay
349
+ setTimeout(() => {
350
+ window.location.href = '/';
351
+ }, 1500);
352
+ } catch (error) {
353
+ console.error('Error saving location:', error);
354
+ this.showMessage('Error saving location. Please try again.', 'error');
355
+ }
356
  }
357
 
358
  cancelLocationSelection() {