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.

modalmap-intro

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 attache 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:

jQuery(document).ready(function () {
	jQuery('.map').click(function (e) {
		jQuery.modal('content of the box');
		return false;
	});
});

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:

'<iframe src="'+this.href+'&output=embed"></iframe><p class="larger"><a target="_blank" href="'+this.href+'">View larger</a></p>'

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:

jQuery(document).ready(function () {
	jQuery('.map').click(function (e) {
		jQuery.modal('<iframe src="'+this.href+'&output=embed"></iframe><p class="larger"><a target="_blank" href="'+this.href+'">View larger</a></p>');
		return false;
	});
});

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

#simplemodal-overlay {
  background-color:#000;
}

#simplemodal-container {
  height:400px;
  width:600px;
  background-color:#fff;
  border:3px solid #ccc;
}

#simplemodal-container iframe{
	height:400px;
	width:600px;
}

#simplemodal-container a.modalCloseImg {
  background:url(images/x.png) no-repeat;
  width:25px;
  height:29px;
  display:inline;
  z-index:3200;
  position:absolute;
  top:-14px;
  right:-18px;
  cursor:pointer;
}

#simplemodal-container p.larger a {
	background:url(images/viewlarger.png) no-repeat;
	display:inline;
	z-index:3200;
	position:absolute;
	bottom:-24px;
	right:-28px;
	cursor:pointer;
	color:#fff;
	padding-left:20px;
	height:20px;
	width:80px;
	text-decoration:none;
	font-size:0.8em;
}

Easy isn’t it?

Related Items and Services:
Solid Wood Flooring at great prices.

6 responses to “Simple Modal + Google map, easy modal map boxes with jQuery”

  1. Eric Martin

    Nice post, Ben. Thanks for using SimpleModal and sharing your work!

    @SimpleModal on Twitter

  2. Peter Ojiambo

    Thanks for the nice post! Having alittle bit of trouble using the technique and id like your help please. I have a picture thumbmail showing the map and link wrapped round it. I have given the link class of “.map” like you suggested, but when I click on the link/image it does not show the modal box and instead goes to the linked address. I have attached all the files and also copied and pasted your js. I notice that “return false” isnt also working, does this mean its a javascript error? can this technique work with an image as a link like i am trying? Appreciate all your help, thanks in advance, your site has been a great resource.

  3. Brett

    great post. i was wondering if you could share the viewlarger.png?

    Thanks so much!

Leave a Reply

  1. CSS Brigit | Simple Modal + Google map, easy modal map boxes with jQuery

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

    Turn any link pointing to a Google map into a nice modal box. Using jQuery, Simple Modal plugin and 5 minutes of your time.

Theme Forest ad
Wordpress Themes Collection ad
Woothemes

Poll

How do you start your WordPress plugins development projects?

View Results

Loading ... Loading ...