I have a map which updates its markers every two seconds. I show toast messages using a 3rd party library that 'something' is wrong at a particular marker. I want the map to be centered and zoomed in to the particular marker when the toast is clicked.
It all works fine for a single marker, but when I have multiple toasts, they all end up showing the last marker (where 'something' did go wrong). I understand this is a problem related to js closures and scopes, but I can't figure out a way to solve it.
if(/*something is wrong at marker*/) {
if(toastShown.indexOf(i) == (-1)) // check if toast has been shown
{
toastShown.push(i); // mark toast for current marker as shown
var err = "Problem detected! Click to go to location";
toastr.error(err, 'Error!', {timeOut: 10000});
problemAtPoint.push(point);
index++;
}
}
for( var j = 0; j < index; j++) {
toastr.options.onclick = (function() {
return function() {
//alert('clicked!');
map.setCenter(problemAtPoint[index]);
map.setZoom(15);
}
})(problemAtPoint[index]);
}