Design Shack - Web design showcase, CSS tutorials and web standards

Follow @designshack on Twitter Like Design Shack on Facebook Find us on Google+ Subscribe to the Design Shack RSS Feed Join our email newsletter

Embedding Google Maps Into a Web Page: A Beginner’s Guide

Written by Joshua Johnson, Published On 27th July 2011.
Filed in HTML.

Tweet
Pin It

Google Maps is one of the best utilities to ever hit the web. It has become the standard way for people to get directions online, view satellite and terrain imagery and perform any other map-related task.

There are a number of reasons that you would want to embed a Google Map into your web page, whether it be for functional purposes, such as guiding users to your physical location, or aesthetic purposes, such as using map for a background graphic. Today we’re going to look at two ways you can go about this task: the quick and easy way and the powerful, flexible API route.


Like the article? Be sure to subscribe to our RSS feed and follow us on Twitter to stay up on recent content.

Embedding a Google Map

The first thing that we’ll learn to do is a simple and straight embed of a Google map. The functionality here is pretty limited but it’s super easy.

The first thing you need to do is simply go to Google Maps like you would if you were looking for directions. Simply type in the address where you want the map to start.

Now click the little link icon in the upper right of the screen. This should pop up a little window that includes a pure link and an HTML snippet, copy the second to your clipboard and paste it into an HTML document.

The HTML

That’s all there is to it! This should give you a live map on your web page. However, you’ll likely never want to paste in the code and be done but instead style it to match your needs.

To do this, let’s take a look at the raw code that results:

<br width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="maps.google.com/maps?client=safari&amp;q=phoenix+az&amp;oe=UTF-8&amp;ie=UTF8&amp;hq=&amp;hnear=Phoenix,+Maricopa,+Arizona&amp;gl=us&amp;ll=33.448377,-112.074037&amp;spn=0.040679,0.07699&amp;z=14&amp;output=embed"></br><br /><small><a href="maps.google.com/maps?client=safari&amp;q=phoenix+az&amp;oe=UTF-8&amp;ie=UTF8&amp;hq=&amp;hnear=Phoenix,+Maricopa,+Arizona&amp;gl=us&amp;ll=33.448377,-112.074037&amp;spn=0.040679,0.07699&amp;z=14&amp;source=embed" style="color:#0000FF;text-align:left">View Larger Map</a></small>

Now, this may look like a big mess but it’s pretty easy to sort through. The links are really the result of all that nasty-looking code so let’s toss those out just for the purpose of the example. Now the code looks much more manageable.

<br width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="LINK"></br><br /><small><a href="LINK" style="color:#0000FF;text-align:left">View Larger Map</a></small>

We can see some pretty basic inline style controls here. For starters, let’s try resizing the map to 600px wide. We can easily wrap that in a div and center it. I’ve also changed the color of the link near the end to white.

<div id="theMap">
    <br width="600" height="350" frameborder="3" scrolling="no" marginheight="0" marginwidth="0" src="LINK"></br><br /><small><a href="LINK" style="color:#fff;text-align:left">View Larger Map</a></small>
</div>

Let’s say we wanted to have a little more fun with this. Let’s ditch that last link completely and set our width to stretch all the way across the page. The code for this is nice and concise.

<br width="100%" height="280" frameborder="3" scrolling="no" marginheight="0" marginwidth="0" src="LINK"></br><br />

Going Fullscreen: The Quick and Dirty Way

The trick above is perfect for embedding a live map into your contact page, but let’s say you wanted to make the map more of a design feature than a utility. It’s pretty easy to use this same technique and stretch the map over the entire screen.

The HTML

To get started, create two divs, one that will hold any artwork, text, etc. and another for the map. I threw in some basic text for the overlay portion and pasted my map into the second div. Notice that I removed the link portion at the end of the embed link as well as the size styles near the beginning of the link.

<body>
 <div id="overlay">
	<h2>Fullscreen Google Map</h2>
	<p>A Design Shack Example</p>
 </div>
 
 <div id="theMap">
     <br frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="LINK"></br><br /><small><a href="LINK"></br><br />
</div>
</body>

That’s really all the markup that we need. Let’s jump over to our CSS to make everything look pretty and get it stacking right.

The CSS

The first thing you’ll want to do is target the br in our map div and set it’s height and width to 100%. This will ensure that our map stretches over the full size of the browser window.

#theMap br {
	height: 100%;
	width: 100%;
}

Update: This works in Safari but it turns out getting Firefox to display a div at 100% height is trickier than I thought. Check out this article for details.

Next, we style the overlay. The key here is to set the position property to absolute, which will pull it out of the normal flow of the document and sit it on top of the map. From here we can style and move it into place.

I wanted a slightly transparent black bar so I used rgba set to 90% opacity. From here I added some margin on the top and set the fonts for both text tags using CSS shorthand.

#overlay {
	background: rgba(0, 0, 0, .9);
	color: #fff;
	margin-top: 150px;
	padding: 20px;
	position: absolute;
	text-align: center;
	width: 100%;
}
 
#overlay h2 {
	font: 200 4em/1.5em "Helvetica Neue", Arial, Helvetica, Geneva, sans-serif;
}
 
#overlay p {
	font: 100 1.2em/1.5em "Helvetica Neue", Arial, Helvetica, Geneva, sans-serif;
}

Get Creative

You can treat the map like any other item on the page, so experiment with changing its shape, applying effects and anything else you can come up with!

The Google Maps APIs

We’ve barely scratched the surface of what you can do with Google Maps on your site. In fact, Google has a whole family of APIs aimed at helping you structure, style and embed highly customized versions of their maps.

The br method above is fun and simple, but the best route to go for embedding maps into your site is probably the JavaScript API. Using this, all you have to do to embed a map is create a div with a specific ID like so:

<body onload="initialize()">
  <div id="map_canvas" style="%; %"></div>
</body>

Then you jump into your JavaScript, call a function, set up some options and store them in a variable, and create the map using the ID from the HTML above.

  function initialize() {
  var mapOptions = {some options here}    
  var map = new google.maps.Map(document.getElementById("map_canvas"),
        mapOptions);

The API provides you with a ton of options for settings like zoom level and which controls are visible. You can even customize the little map markers with your own images.

API Tutorials

If you wanted to get started with the Google Maps APIs, here are some links that you’ll definitely want to check out.

  • Official Google JavaScript API Tutorial
  • Wade Hammes: Setting a Google Map as the Background of your Web Site

Static Maps

Don’t want an interactive map? Check out the documentation for adding a static map. Many of the features are the same, the result is simply a non-moving image rather than a real map that users can play with.

Conclusion

To sum up, if you want a Google Map on your site with no effort, follow the steps above to go the br route. Your control over the map is limited but you can have a lot of fun with how you embed it in the page. If you need more features and flexibility, sign up for a free API key and go the JavaScript route.

Leave a comment below and show us any pages you’ve created with Google Maps. What techniques and resources have you found helpful?

Tweet
Pin It

Discussion

  1. spacer Eric W. says:
    July 27, 2011 at 9:38 pm

    Excellent, just started taking a more active roll in the website for a fake film trailer contest my friends put on here in Portland Or. First thing on the list is plug into Google maps. Your guide outlines the how to very well.

    Cheers,

    Eric

  2. spacer Kim Ruddock says:
    July 28, 2011 at 7:38 am

    Embedding Google Maps is a very useful way of enhancing a site’s function and usability. I always take out the tag – not exactly sure why it’s in there to be honest. I like the idea of being creative with the map and changing the shape of it rather than just placing it as standard rectangle within a design. With a bit of thought it could become a significant design feature as well as a functional element.

  3. spacer Etube says:
    August 24, 2011 at 8:15 pm

    Hello, and how it adds the new google map?
    google maps api 3? Anyone know how?
    Thanks ..

  4. spacer datta says:
    February 3, 2012 at 9:50 pm

    how to reduce pop address window width

Leave Your Reply

Click here to cancel reply.

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.