Here is my markers[] array:
partnerMarkers = [
// London
{ lat: 51.515482718, lng: -0.142903122, name: "London", content: "Our home town and international hub." },
// Dubai
{ lat: 25.2644444, lng: 55.3116667, name: "Middle East", content: "Dubai desc" }
];
I have this function, looping through the array of markers (triggered by a button elsewhere):
function toggle_layer(markers) {
for (var i=0; i<markers.length; i++) {
markers[i].setVisible(false);
}
}
I get markers[i].setVisible is not a function – but then this works fine:
function toggle_layer(markers) {
for (var i=0; i<markers.length; i++) {
console.log(markers[i]);
}
}
Why does setVisible not work in this context?
Here is the JSFiddle Demo:
Seems like your markers are just objects instead of
google.maps.Markers, and thus it does not havesetVisible()function within it. You basically want to convert the data within your Object into agoogle.maps.Markerobject. I created a global arraygooMarkerto hold the Markers. By clicking on the link below the map, it’ll hide the markers. Here is the way to create Markers and then hide them:HTML Markup:
JavaScript + Google Map V3 API: