Skip to content

Instantly share code, notes, and snippets.

@faffyman
Created March 28, 2012 20:00
Show Gist options
  • Save faffyman/2230007 to your computer and use it in GitHub Desktop.
Save faffyman/2230007 to your computer and use it in GitHub Desktop.
Auto embed a google map from it's share link
// --------------------------------------------------------
// auto embed google map if google map link found in body
// Allows site content editors to simply place a standard
// link in their text, and this will turn it into an
// embedded map
// --------------------------------------------------------
// N.B. links with a class of dont-embed means... duh. dont embed.
var sMapSel = 'a[href^="http://maps.google.co.uk/maps"]:not(.dont-embed)' ;
$(sMapSel).each( function() {
var nEmbedMapWidth = 578 ; // change dimensions to suit web page design
var nEmbedMapHeight = 350 ;
var sEmbedMapHref = $(this).attr('href') ;
// swap source=embed to output=embed
sEmbedMapHref = sEmbedMapHref.replace('source=embed','output=embed');
// ensure output = embed exists
if(sEmbedMapHref.indexOf('output=embed')=== -1 ) {
sEmbedMapHref = sEmbedMapHref + '&output=embed' ;
}
// Ok we have the width/height and href
// transform this into an iFrame embed tag
// FORMAT is
//
// <iframe frameborder="0"
// height="350"
// width="578"
// marginheight="0"
// marginwidth="0"
// scrolling="no"
// src="http://maps.google.co.uk/maps?q=Address+Again&amp;output=embed" />
//
if( sEmbedMapHref !='' && typeof(sEmbedMapHref)!='undefined') {
var embedMapHtml = '<'+'ifr'+'ame ';
embedMapHtml+= 'width="'+nEmbedMapWidth+'" height="'+nEmbedMapHeight+'" ';
embedMapHtml+= ' src="'+sEmbedMapHref+'" ';
embedMapHtml+= ' frameborder="0" marginheight="0" marginwidth="0" scrolling="no" ';
embedMapHtml+= ' >'+'<'+'/'+'ifr'+'ame'+'>';
$(this).css("display","block") ;
$(this).parent().before(embedMapHtml); // assumes the link is in a <p> or <div>
//$(this).hide(); //option to hide teh original link, or leave in place as a "larger version" link.
}
});// end each link
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment