Right now my code looks like this:
// create a LatLng array out of given coordinates string
for (var i = 0; i < coordinateString.length; i++) {
var polygonCoords = new Array();
var j = 0;
var z = j + 1;
while (z < coordinate.length)
{
if ((j%2) === 0)
{
var coord1 = parseFloat(coord[z]);
var coord2 = parseFloat(coord[j]);
var newLatLng = new google.maps.LatLng(coord1,coord2);
polygonCoords.push(newLatLng);
} else
{
var coord1 = parseFloat(coord[j]);
var coord2 = parseFloat(coord[z]);
var newLatLng = new google.maps.LatLng(coord1,coord2);
polygonCoords.push(newLatLng);
}
z++;
j++;
}
/** Adds the polygon to a polygon array
* and maps it onto the map
*/
var newPoly = new google.maps.Polygon(
{
paths: polygonCoords,
strokeColor: objArray[i].borderColor,
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: objArray[i].fillColor,
fillOpacity: 0.35
})
newPoly.setMap(map);
newPoly.set("eventNum",objArray[i].eventID)
newPoly.set("offTime", objArray[i].offTime)
function attachInfoWindow(event)
{
var infowindowPoly = new google.maps.InfoWindow();
var eventNo = newPoly.get("eventNum");
var outTime = newPoly.get("offTime");
var resTime = objArray[i].restoreTime;
var contentString = "Event Number: " + eventNo + "<br> Outage Time: " + outTime + "<br> Estimated Restoration Time: " + resTime;
infowindowPoly.setContent(contentString);
infowindowPoly.setPosition(event.latLng);
infowindowPoly.open(map);
google.maps.event.addListener(map, 'click', function()
{
infowindowPoly.close();
});
}
google.maps.event.addListener(newPoly, 'click', attachInfoWindow);
}
}
Which creates an array of polygons in different positions (its looping, so everytime a new polygon is created and set to the map) and when clicked an infowindow pops up and displays some content, except it is the same content. However I want to display different infowindows for each polygon. I've read all the other similar questions and answers and have modified my code.
Note that, I've stored objects holding the content I want into an array and am using indexes and other ways to grab properties of the object and then display it as my infowindow content.
Create a function to return the click listener function. Pass the index and the polygon into that function so you get function closure on them to keep them associated:
Call the function like this:
proof of concept fiddle
code snippet: