Salutations good people,

Been hacking away at Mashup for far too long on one issue after the next.

Now my program works almost exactly as the staff solution, but I've noticed when dragging/zooming the map on my solution that old markers remain. The staff solution, however, seems to remove old markers as you move the map around, allowing only 10 markers at a time.

Where am I going wrong? Was digging through previous pset8 questions to find clues, but everyone seems to be approaching the problem from different philosophical standpoints.. Now I'm even more confused than before.

Here are my relevant functions:


// Add marker for place to map
function addMarker(place)

    // instantiate variables
    var latlng = {lat: place.latitude, lng: place.longitude};
    var content = [];
    var marker = new google.maps.Marker({
        position: latlng,
        map: map

    // get articles for place
    $.getJSON("/articles", {geo: place.postal_code}, function(data){
            for (var i = 0; i < data.length; i++){
                markers[i] = marker;
                content += '<li>' + '<a href="'+data[i].link+'">' + data[i].title + '</a>' + '</li>';

    // listen for clicks on marker
    google.maps.event.addListener(marker, "click", function(){
        showInfo(marker, content);


// Remove markers from map
function removeMarkers()
    // loop through markers, removing and deleting from array
    for (let i = 0; i < markers.length; i++){
        markers[i] = null;
    markers.length = 0;

1 Answer 1


The removeMarkers will get rid of one marker, the last one added. The problem is in addMarker. addMarker will be called for each "place" and should add one marker to the map and one marker to the markers array. Look at where this line markers[i] = marker; is and you should see that the markers array is incorrect.

  • Thanks for the clue, Dino! Went with the push() method instead, and it seems to have worked perfectly. Question though--WHY? Was my original way of going about it not exactly the same functionality, sans object-orientation?
    – Foxhole
    Commented Apr 9, 2018 at 1:00
  • Because markers[i] = marker; is (was?) in the articles loop, markers[0] through markers[data.length] are all being populated with the current marker. After place iterations, it holds the last marker data.length times. Commented Apr 9, 2018 at 11:57
  • Hah of course! Thanks again,
    – Foxhole
    Commented Apr 9, 2018 at 13:29

You must log in to answer this question.

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