I want to display an alert with the marker info when I click on it. I receive the parameters as a json and my problem is when I click any marker, it is always displaying the info of the last marker. I don´t know where the problem can be.
var map = new google.maps.Map(document.getElementById('map'),
mapOptions);
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
var userPos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude)
var markUsr = new google.maps.Marker({
position: userPos,
map: map,
icon: 'images/locblue.png'
});
map.setCenter(markUsr.position);
for (var i = 0; i < data.length; i++) {
var pos = new google.maps.LatLng(data[i].latitud, data[i].longitud)
var marker = new google.maps.Marker({
position: pos,
map: map,
icon: 'images/locred.png',
description: data[i].desc
});
var infowindow = new google.maps.InfoWindow({
content: data[i].name
});
infowindow.open(map, marker)
google.maps.event.addListener(marker, 'click', function () {
alert(marker.description)
})
}
})
}