I have everything working with addMarker()
except for the fact that the infoWindow pops up in the wrong spot, as in the last marker that was loaded. I found the link below which seems relevant and the advice makes sense, however, the addMarker()
function is already removed and the loop runs in the update()
function.
https://stackoverflow.com/questions/5736691/google-maps-infowindow-showing-on-wrong-marker
Here is my code which has the articles and everything popping up.
function addMarker(place) { var latLng = new google.maps.LatLng(place.latitude, place.longitude);
marker = new MarkerWithLabel({
position: latLng,
draggable: true,
raiseOnDrag: true,
map: map,
icon: '../img/news-icon.png',
labelAnchor: new google.maps.Point(27, 60),
labelClass: "labels",
});
google.maps.event.addListener(marker, "click", function() {
showInfo(marker);
$.getJSON("articles.php", {
geo: place.postal_code,
})
.done(function(data, textStatus, jqXHR) {
var numArticles;
if (data.length < 6){
numArticles = data.length;
}
else{
numArticles = 6;
}
if(numArticles === 0){
showInfo(marker, "No News Today");
}
else{
var articles = "<ul>";
for(i = 0; i < numArticles; i++){
articles += "<li><a href=" + data[i].link + ">" + data[i].title + "</a></li>";
}
articles += "</ul>";
showInfo(marker, articles);
}
})
})
markers.push(marker);
};