Use a Google Map as the background of your web page

Ever want to fill up the screen with a Google Map and make it the background? We wanted to do that for the location pages on Lexoo.

Here's an example of what I'm talking about:

Turns out it's pretty easy to do.

Let's say you have basic markup like this:

<!DOCTYPE html>
<html>
  <head>
    <link href="/test/application.css" media="screen" rel="stylesheet" type="text/css" />
    <script type="text/javascript"
      src="https://maps.googleapis.com/maps/api/js?sensor=false&key=YOUR_API_KEY">
    </script>
    <script src="/test/application.js" type="text/javascript"></script>
  </head>
  <body>
    <div id='container'>
      <h1>OMG, this is ON TOP OF THE MAP!</h1>
    </div>

    <div id='map-canvas'></div>
  </body>
</html>

And the standard javascript like:


$(document).ready(function() {
 var mapOptions = {
    zoom: 8,
    center: new google.maps.LatLng(-34.397, 150.644),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
}

Then what you want to do is absolutely position the map, and pin it to the top/left/right/bottom sides. The container div is where you put everything that you want to float on top of the map.

#map-canvas {
  height: 100%;
  position: absolute; 
  top: 0; 
  bottom: -200px; 
  left: 0; 
  right: 0; 
  z-index: 0;
}

#container {
  z-index: 100;
  position: relative;
}

And boom - you've got a Google Map in the background that looks like:



Subscribe via RSS

Back to all blog posts