1

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);
};

1 Answer 1

4

Had the same trouble. Fix it with 'var' before 'marker = new...'. Else you overwrite the same variable each time.

3
  • Thanks, you rock <3 Commented Oct 9, 2017 at 13:29
  • Thanks for your answer. Helps me with this issue!!
    – Amber C.
    Commented Aug 14, 2018 at 3:13
  • thank you so much
    – povoctober
    Commented Mar 13, 2019 at 18:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .