admin管理员组文章数量:1391929
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 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?
Share Improve this question edited Mar 31, 2011 at 12:33 strangerpixel asked Mar 31, 2011 at 12:08 strangerpixelstrangerpixel 8281 gold badge12 silver badges27 bronze badges 3- What kind of object is markers? – Niklas Wulff Commented Mar 31, 2011 at 12:10
- @NiklasRingdahl it's an array. – strangerpixel Commented Mar 31, 2011 at 12:26
- Yeah, but an array of what? I suppose it's supposed to be of Marker, could you provide the code for when the markers object is set? – Niklas Wulff Commented Mar 31, 2011 at 12:28
3 Answers
Reset to default 2Here is the JSFiddle Demo:
Seems like your markers are just objects instead of google.maps.Markers
, and thus it does not have setVisible()
function within it. You basically want to convert the data within your Object into a google.maps.Marker
object. I created a global array gooMarker
to 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:
<div id='parent'>
<div id="map_canvas" style="width: 650px; height: 550px;"></div>
</div>
<div id='hidemark'>Click to hide markers</div>
JavaScript + Google Map V3 API:
var map;
var gooMarker = [];
var partnerMarkers = [
{
lat: 51.515482718,
lng: -0.142903122,
name: "London",
content: "Our home town and international hub."},
{
lat: 25.2644444,
lng: 55.3116667,
name: "Middle East",
content: "Dubai desc"}
];
function initialize() {
var london = new google.maps.LatLng(51.5, 0);
var myOptions = {
backgroundColor: '#FFFFF',
zoom: 2,
center: london,
navigationControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map_canvas = document.getElementById("map_canvas");
map = new google.maps.Map(map_canvas, myOptions);
for (var i = 0; i < partnerMarkers.length; i++) {
gooMarker.push(new google.maps.Marker({
position: new google.maps.LatLng(partnerMarkers[i].lat, partnerMarkers[i].lng),
map: map,
title: partnerMarkers[i].name
}));
}
}
function hideMarkers(){
for(var i=0; i<gooMarker.length; i++){
gooMarker[i].setVisible(false);
}
}
document.getElementById('hidemark').onclick = hideMarkers;
window.onload = initialize;
kjy112 is on the spot, simplified to plugin direct to your code:
partnerMarkers = [
// London
new google.maps.Marker(
{ position: new google.maps.LatLng(51.515482718, -0.142903122),
title: "London - Our home town and international hub." },
// Dubai
new google.maps.Marker(
{ position: new google.maps.LatLng(25.2644444, 55.3116667),
title: "Middle East - Dubai desc" }
];
I'm not seeing the Marker constructor in your code
new google.maps.Marker
本文标签: javascriptGoogle Maps setVisible is not a functionStack Overflow
版权声明:本文标题:javascript - Google Maps setVisible is not a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744712968a2621234.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论