How to integrate Fancybox and Google Maps

Written by on November 23, 2010 in jQuery - 5 Comments
map1

One reason I love Fancybox is how versatile it is. I was coding a website for a client and they wanted to have a Google Map on their contact page. The problem was that the contact page already had a ton of content; phone numbers, contact forms, etc. They didn’t want a separate page for the map though, so I came up with an idea. My thought was that if I made a small graphic of a map / button for the sidebar of the contact page, then I could use Fancybox to open the Google Map over the screen when the user clicked the button. This would keep the page looking oh-so-clean, while still having all the info for the user.

Check out the demo to see an example of this in action.

The demo shows an example of having a smaller graphic representing the map that opens the Google Map when clicked on.

In order to do this you need to first download Fancybox and hook it up to your page.

Next put this code in your head.

<script type="text/javascript">
		$(document).ready(function() {
	
		$("a#inline").fancybox({
		'hideOnContentClick': true,
		'overlayColor'		: '#6b0202',
		'overlayOpacity'	: 0.8
		});
	});
</script>

The overlay color and opacity are optional, that’s for the dark red background when the map is overlaid over the webpage.

So what is actually happening on the page? The Google Map is in a div with an ID of mapBox. I’m using CSS to hide the div so you don’t see it on the page under normal conditions. Then the javascript is being used to show the div, and open it with the Fancybox styling. Here is the CSS to make this hapen:

#mapBox {
	width:600px; 
	height:510px;
	display:none; 
	*margin-left:-9999px; 
	*height:0px; 
	*position:relative; 
	*top:-500px; 
	*display:block
}

Looking at the code you’ll see there is some Internet Explorer specific code. This is because there is a problem getting the map to show up in IE without setting the box to display:block. The rest of the IE specific code is shoving the div with the map off the screen so you don’t see it until you open it in Fancybox. IE hacks are annoying, but this got the job solved so it functions correctly across browser, so I’m happy.

Here’s how the link in the XHTML looks (telling the div with the ID of mapBox to open when clicked):

<a id="inline" href="#mapBox" >
<img src="blahblah" alt="blahblah" />
</a>

Grab the download of the demo, and have fun! Go Sox!

Sponsors

About the Author

Mike Ilsley "ilz" is a Boston based website designer, front-end developer, WordPress addict, owner of Beantown Design, and exclusive ThemeForest author. Follow him on Twitter and Facebook, and don't forget to subscribe to the WebDesign&Such email RSS feed. Also, check out his latest project, jQueryMobileShowcase.com.


  • Scott

    A much easier approach than using the API, but it only seems to work once. If you close the box and then open it again the map does not load, you have to refresh the page to make it work again.

  • Kaka

    Hi, i like this style, but i not understand how to make this insite

  • http://www.jakub.chodorowicz.pl/ chodorowicz

    Thanks so much for that! I’ve been strugling with using FancyBox+GoogleMapsEmbedf and the problem with centering and your solution is the only working one I’ve found.

    Greets!

  • Italy

    It work only one time. Try to open fancy, close and reopen…

  • http://www.jakub.chodorowicz.pl/ chodorowicz

    @Italy
    Works for me every time – you can check it out here: http://www.luka.pl/new/