Colorbox with Google maps

by Giorgos Grispos on June 8, 2011

Hello,

Any time I needed a lightbox effect for my projects I end up with colorbox.

It’s  really easy to install it to your project, stable and not really heavy, supports all normal functionality and follows W3C standards.

So while working on a new project Pallas restaurant which I am going to provide you more information in a later post, I needed a lightbox plugin which allow me to use Google maps within it. The question is how Lightbox and Google maps come together?  Have a look on the script below.

$(document).ready(function () {
  $(".fMap").colorbox({
    html:'<div id="map_canvas_all" style="width:600px; height:450px;"></div>',
    scrolling:false,
    width:"600px",
    height:"470px",
    onComplete:function(){ loadScriptGeneral(); }
  });
});

That is the main load function where “.fMap” is the link to open the Google maps.
My example also has custom images as markers on the map.  As you can see it includes two pngs one for the marker one for the shadow of it. It’s even simpler if you don’t need any custom marker icon.

function loadScriptGeneral() {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initializeGeneral";
  document.body.appendChild(script);
};

function initializeGeneral() {
  var imageG = new google.maps.MarkerImage('/media/imgs/google.png',
      new google.maps.Size(20, 26),
      new google.maps.Point(0, 0),
      new google.maps.Point(0, 26)
  );
  var shadowG = new google.maps.MarkerImage('/media/imgs/shadow.png',
      new google.maps.Size(23, 30),
      new google.maps.Point(0, 0),
      new google.maps.Point(0, 30)
  );
  var myLatlngG = new google.maps.LatLng(35.518421,24.018758);
  var myOptionsG = {
      zoom: 16,
      center: myLatlngG,
      mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var mapG = new google.maps.Map(document.getElementById("map_canvas_all"), myOptionsG);

  var markerG = new google.maps.Marker({
      position: myLatlngG,
      map: mapG,
      title: "Παλλάς",
      shadow: shadowG,
      icon: imageG
  });
};

The first piece of script “loadScriptsGeneral” loads the general Google maps script. Once it’s loaded callback the “initializeGeneral” function. At the end you place the Google map object to the “map_canvas_all” div which we created at the biggining of the script above.

It’s a really simple method to add you google maps to your site within a lightbox effect. I hope it helps some of you and please post any additions or recommendations.

8 comments

Yes that’s right, it works fine like this. Thank you colorbox and Giorgos! have a look at http://www.pallaschania.gr to see it in action

by John on 11/09/2011 at 12:13. Reply #

one year later……

I got this warning from firebug:

Warning: you have included the Google Maps API multiple times on this page. This may cause unexpected errors.

main.js

by ArthasMX on 24/08/2012 at 22:20. Reply #

I did the following and worked great:
Please note {entry_id} is an expressionengine tag, I have it there so I can have multiple google maps on one page.

Not that I am including the Google Map JS separately not like in the example.

$(document).ready(function () {
$(“#google_map{entry_id}”).colorbox({
html:”,
scrolling:false,
width:550,
//height:”470px”,
onComplete:function(){ loadScriptGeneral{entry_id}(); }
});
});

function loadScriptGeneral{entry_id}() {
var myLatlng = new google.maps.LatLng(‘{google_latitude}’,'{google_longitude}’);
var myOptions = {
zoom: 15,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}

var map = new google.maps.Map(document.getElementById(“map_canvas{entry_id}”), myOptions);

var contentString = ”+
”+
”+
‘{title}’+
”+
‘{street_address}, {suburb}, {state} {postcode}’+
Phone: {phone_number}‘+
”+
”;

var infowindow = new google.maps.InfoWindow({
content: contentString
});

var marker = new google.maps.Marker({
position: myLatlng,
animation: google.maps.Animation.DROP,
map: map,
title:”{title}”
});

google.maps.event.addListener(marker, ‘click’, function() {
infowindow.open(map,marker);
});
};

by Trent on 02/09/2012 at 10:23. Reply #

That’s a great idea Trent, entry_id is a useful tip for those who need multiple instances in one page.
Thanks for sharing your thoughts! Giorgos

by giroin on 07/09/2012 at 13:22. Reply #

A fair bit of code got stripped out of the above.
Txt file here – http://www.kubikdesign.com.au/googlemap-color-box.txt

by Trent on 02/09/2012 at 10:26. Reply #

Thanks, I was able to adapt your sample to my needs ;)

by Citronas on 26/12/2012 at 18:57. Reply #

I am getting this error on Firebug Console.. not sure what’s wrong.

TypeError: window.initializeGeneral is not a function

by Adrian on 20/03/2013 at 03:03. Reply #

Hi Adrian, is there any test URL to have a look?

by gio on 20/03/2013 at 11:47. Reply #

Leave your comment

Required.

Required. Not published.

If you have one.