From 21ce7689c2d815d189ec6b4b9567a1f5670b19dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Mon, 24 Apr 2023 17:43:44 +0200 Subject: [PATCH] Don't overwrite marker when creating investment markers The `marker` variable is like a global variable inside the `initializeMap` function, so assigning it inside the `createMarker` function was changing its value in other places. So we're using different variable names like `newMarker` in order to make the code easier to follow. Now we "only" change the `marker` variable in functions that modify the marker. --- app/assets/javascripts/map.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/app/assets/javascripts/map.js b/app/assets/javascripts/map.js index 06ddd619a..3356338f8 100644 --- a/app/assets/javascripts/map.js +++ b/app/assets/javascripts/map.js @@ -29,19 +29,19 @@ html: '
' }); createMarker = function(latitude, longitude) { - var markerLatLng; + var newMarker, markerLatLng; markerLatLng = new L.LatLng(latitude, longitude); - marker = L.marker(markerLatLng, { + newMarker = L.marker(markerLatLng, { icon: markerIcon, draggable: editable }); if (editable) { - marker.on("dragend", function() { - App.Map.updateFormfields(map, marker); + newMarker.on("dragend", function() { + App.Map.updateFormfields(map, newMarker); }); } - marker.addTo(map); - return marker; + newMarker.addTo(map); + return newMarker; }; removeMarker = function(e) { e.preventDefault(); @@ -79,10 +79,12 @@ } if (addMarkerInvestments) { addMarkerInvestments.forEach(function(coordinates) { + var investmentMarker; + if (App.Map.validCoordinates(coordinates)) { - marker = createMarker(coordinates.lat, coordinates.long); - marker.options.id = coordinates.investment_id; - marker.on("click", App.Map.openMarkerPopup); + investmentMarker = createMarker(coordinates.lat, coordinates.long); + investmentMarker.options.id = coordinates.investment_id; + investmentMarker.on("click", App.Map.openMarkerPopup); } }); }