JQuery FancyBox with Google Maps

爷,独闯天下 提交于 2019-12-01 07:32:46
JFK

It seems to be an issue (bug?) when google maps is initialized in a hidden div (can be the same case when using tabs) since display:none may set its width and height to 0.

When the inline map is visible, fancybox is able to compute its dimensions, hence it works when you remove display:none.

The workaround should be resizing the map once the fancybox is already opened so the map will fit into the fancybox dimensions. You can use the onComplete callback for that.

Other things to bear in mind:

  • You may need to set autoDimensions either true or false depending on whether the selector #mapcontainer has css dimensions or not (set to false if not, otherwise set to true.) I would think it has dimensions since you can display the map inline so the initial value would be true.
  • Since you are using inline content (fancybox v1.3.x) beware of this bug. The same link also shows the workaround.

So your fancybox custom script should look like:

 $("a#inline").fancybox({
  'hideOnContentClick': false, // so you can handle the map
  'overlayColor'      : '#ccffee',
  'overlayOpacity'    : 0.8,
  'autoDimensions': true, // the selector #mapcontainer HAS css width and height
  'onComplete': function(){
    google.maps.event.trigger(map, "resize");
  },
  'onCleanup': function() {
   var myContent = this.href;
   $(myContent).unwrap();
  } // fixes inline bug
 });

the method to resize the map might be different depending on the version of google maps API you are using

You can check https://stackoverflow.com/a/2590049/1055987 for further reference.

UPDATE: I have added a DEMO here

watercolor12

It's the hideContentOnClick parameter. Try setting that parameter to false.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!