I am currently facing an issue with Google markers in the Maps API.
Below is the code snippet from an AJAX success function (where 'map' is a parameter containing this code):
success: function(data) {
var tmpmap = map; // Necessary for adding markers to the map
ris = JSON.parse(data); // Parsing JSON data
for (var i = 0; i < ris.length - 1; i++) {// Looping through the data
var id = ris[i].id_sede; // Retrieving ID from JSON
var marker = new google.maps.Marker({ // Creating a marker
map: tmpmap,
mid: id, // Issue arises here
position: {
lat: parseFloat(ris[i].latitude),
lng: parseFloat(ris[i].longitude)
}
});
The data retrieved by AJAX consists of a standard SQL query containing latitude, longitude, ID, and additional information.
The problem I'm encountering is that each marker created during the loop assumes the value of the current "mid" (map ID).
In short, if I have 4 markers with IDs 1, 2, 3, and 4, by the end of the loop, all markers end up having the ID of 4 instead of 1, 2, 3, and 4 separately.
My main question is: How can I prevent this behavior? Am I creating markers correctly? And why do changes to one marker affect all others?