最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Maps setVisible is not a function - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 2

Here 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
发布评论

评论列表(0)

  1. 暂无评论