Simple Modal + Google map, easy modal map boxes with jQuery

Simple Modal + Google map, easy modal map boxes with jQuery

If you don’t know what Modal box is referring to, think about Lightbox. That’s it, the ‘new way’ (rather old now) of displaying pop-up boxes. Their are many scripts to add modal boxes to your content. Some very fancy, with loads of options and effects, others very simple and light.

I needed to display  Google maps for a client in one of those modal box. After 5 min searching I stumbled across SimpleModal, a jQuery plugin that works quite well and is very light-weight.

The brief is as follow: I’ve got a link that point to a google map on google’s site. I want that in a modal box displayed when a user click on a link.

The modal box will be done by SimpleModal so no code needed here. Download the file and include it in your page header. Now we need to attach an event to every link that point to a map to trigger the modal box. To do that I’ve added a class to each link called ‘map’ and added a listener on each of them to trigger a function. In jQuery this looks like that:

[snippet-listener]

Now to get the google map to display without anything around there is a simple trick: add ‘&output=embed’ at the end of the url.

The content in the box is something like this:

[snippet-href]

this.href gets you the href value of the link, in our case the googlemap link, then we add the &output=embed to get rid of the rest of the page. After the iframe I’ve added a link to the map in case the user wants a larger version. This link open in a new window with the target blank.

So the complete code:

[snippet-complete]

And a bit of css, taken from the plugin site. I’ve also added a bit to style up the view larger button:

[snippet-Css]

Easy isn’t it?

1 thought on “Simple Modal + Google map, easy modal map boxes with jQuery”

Leave a Reply

Your email address will not be published. Required fields are marked *