I have my webpage where in the map i have some markers loaded from a DB, these ones load correctly and i have an addListener('click') for each of these markers that opens a little window with infowindow.open(), the thing is that i want this window to load with the correct information from the element of the DB that corresponds to the marker i clicked, i've tried to pass the value in diferent ways and using diferent variables, but the only thing i get is undefined or the last element of the DB, and what i want is the information of the selected marker.
Code here:
<script>
var markers = @json($info);
var lat = [];
var lng = [];
var titol = [];
var adreca = [];
for(var i = 0; i < markers.length; i++){
lat[i] = markers[i].lat;
lng[i] = markers[i].lng;
titol[i] = markers[i].nom_rocodrom;
adreca[i] = markers[i].adreca;
}
function initMap() {
var centre = {lat: 41.731047, lng: 1.783573};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: centre
});
for(var i = 0; i < markers.length; i++){
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat[i],lng[i]),
map: map,
animation: google.maps.Animation.DROP,
id: i
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.open(map, marker);
}
})(marker, i));
var infowindow = new google.maps.InfoWindow({
content: '<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<h3 id="firstHeading" class="firstHeading">'+titol[marker.id]+'</h3>'+
'<img src="">'+
'<div id="bodyContent">'+
'<p>'+adreca[marker.id]+'</p>'+
'<button type="button" class="btn btn-primary">'+'<a class ="boto_roco" href="/rocodroms/{{ $rocodrom->id }}">Informacio</a>'+'</button>'+
'</div>'+
'</div>',
maxWidth: 200
});
}
}
The strange thing is that if i console.log(marker.id) inside here:
return function() {
infowindow.open(map, marker);
}
the id value that i get is the correct one but if i then use marker.id inside the content part i get undefined.