Content installation issue

So, I have a few markers popping up from an array, but I have a lot of difficulty customizing the content of the InfoWindows displayed

I put the headers in which works fine, but when I try to set the content, it doesn't set the content, but it still shows the header.

What am I doing wrong?

for (index = 0; index < data.length; ++index) {
    locationName = data[index].LocationName;
    locationID = data[index].LocationID;
    locationX = data[index].XCord;
    locationY = data[index].YCord;
    var infoContent = '<div><h3>' + data[index].LocationName + '</h3></div><div>' + data[index].LocationID +  '</div>';
    var mapLocation = new google.maps.LatLng(locationX,locationY);
    var marker = new google.maps.Marker({ // Set the marker
        position: mapLocation, // Position marker to coordinates
        icon: image, //use our image as the marker
        map: map, // assign the marker to our map variable
        title: data[index].LocationName,
        content: infoContent
        //draggable:true //Have a guess what this does
    });

    // Allow each marker to have an info window    
    google.maps.event.addListener(marker, 'click', (function (marker, index) {
        return function () {
            infoWindow.setContent(infoContent);
            infoWindow.setContent(this.title);
            infoWindow.open(map, this);
        }
    })(marker, index));

    var infoWindow = new google.maps.InfoWindow({ 
        content: infoContent 
    }), marker, index;
}

      

+3


source to share


1 answer


You are calling setContent

twice:

infoWindow.setContent(infoContent);
infoWindow.setContent(this.title);

      

I think the latter should be removed.

Edit:



To get the correct infoWindow content, try moving the eventListener creation into a function:

// Allow each marker to have an info window    
updateInfoWindowOnMarkerClick(marker, infoContent); 

//Somewhere after the loop
function updateInfoWindowOnMarkerClick(marker, infoContent){
    google.maps.event.addListener(marker, "click", function () {
        infoWindow.setContent(infoContent);
        infoWindow.open(map, this);
    });      
}          

      

Check out JavaScript Closing Inside Loops - A Simple Practical Example for more information on function scope.

+1


source







All Articles