开发者

Google Markers setVisible true/false show/hide

Ok, so I have a google Map store locator. Input a post code, it returns stores in a radius. Got all these pretty info windows, marker icons, and can even do directions...

In a nutshell, I need to be able to HIDE (which I can do) all the markers before I calculate directions (because the directions api seems to automatically add its own markers which I have no 开发者_如何学JAVAidea how to override).

At the moment I'm using the following to HIDE the markers. This works 100%. It sets the visible value of the marker to false in the DOM tab of firebug and the marker icons disappear.

  function clearMarkers() {

    for (var i = 0; i < markers.length; i++) {
      markers[i].setVisible(false);
    }
  }

Now, my issue is I cannot SHOW OR unhide the markers. This is my current function. I can still click on my listing of stores and it pops up the markers infoWindow, BUT the marker just doesn't appear once its been hidden. Can anyone help me in unhiding the marker ICON in this restoreMarkers function?

  function restoreMarkers() {
    for (var i = 0; i < markers.length; i++) {
      markers[i].setVisible(true);
    }
    markers.setMap(map);
  }

Here's all my code on jsFiddle if you need it


Just for everyone's info:

The setMap call at the end of the restoreMarkers function is false! Without verifying, i'd bet this triggered the conflict as the object that shall be set to the map is already there - it's just not visible (imagine of the difference between display:none and visibility:hidden in CSS).

The setVisible method affects only the visibility - not the existence. So setting setVisible to false, will not remove the marker object from the map object, but only makes it invisible.

More information and detailed tutorials about toggling MVC objects and the JavaScript Maps API v3 can be found at my blog. For example check out my latest: https://web.archive.org/web/20111011133754/https://bexton.net/2011/07/11/tutorial-advanced-google-maps-marker-toggle-with-jquery-and-maps-api-v3/


Without having tried it I can think of one possible thing that could go wrong. Is it possible that the directionsrenderer places another layer on top of the map that hides all the markers? Have you tried removing it to see whether that would bring back the markers?

I have used the setVisible(true) many times without any problems, so if the directionsdisplay is not the problem I am at a loss. Any chance you could link to the homepage where the code runs so we could try it out?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜