October 13th 2009 by Peter · 162 comments
Adding Custom Google Maps to Your Website
Maps are often placed on a company website to help customers find their way there. For that, Google Maps is excellent. But wouldn’t it be nice to add your company logo, parking lots, train stations, etc. to the map, to help the customer even more? It is very simple, and in this article I am going to show you how.
Before we start, check out what we are going to create:
Now, here is an overview:
Overview
- Google Maps API
- Getting the Coordinates
- Adding the Map to Your Website
- Adding Markers
- Customizing the Markers
- Adding Infoboxes
- Download
Google Maps API
The Google Maps API allows you to embed maps directly into your website. All it takes is a little JavaScript, and for beautifying—a little CSS. Version 3 of the Google Maps API has just been released, and of course, that is what we will be using here. You can read the entire documentation over at Google Labs, and while you are there, be sure to get an API key.
Getting the Coordinates
As I do not expect you to know the precise coordinates of your location, I will explain a very quick way Google has provided to do this. When you know the exact address, you can put it in an URL of this form:
http://maps.google.com/maps/geo?q=1+Infinite Liip,+Cupertino,+CA+95014,+USA&output=csv&oe=utf8&sensor=false&key=your_google_maps_api_key
When you enter this in your address bar, you will see this:
The first number is the status code, and 200 means that everything is okay. The second number shows how accurate the address is—in this case the number is 8, which is good. The last two numbers are latitudes and longitudes, which are the numbers we need.
Adding the Map to Your Website
There’s no need to hesitate – let’s add that map to your website! Open your favorite HTML editor and create a standard HTML file with UTF-8 encoding. First of all, we have to create the viewport and tell our HTML file to get the JavaScript file from Google Code. Add these lines between <head> and </head>:
1 2 | <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> |
After the URL, you will notice sensor=false. As we do not use any sensor, such as a GPS, to locate the location, this is set to false.
Just below what we have just inserted, write the following:
1 2 3 4 5 6 7 8 9 10 11 12 | <script type="text/javascript"> function initialize() { var latlng = new google.maps.LatLng(57.0442, 9.9116); var settings = { zoom: 15, center: latlng, mapTypeControl: true, mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}, navigationControl: true, navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL}, mapTypeId: google.maps.MapTypeId.ROADMAP }; |
Let’s split this up to ensure that we understand it fully. In line 2 we create the function initialize(). Inside this function we are going to define the basic settings of the map. In line 3 we create a new variable, latlng. latlng stands for latitudes and longitudes. The variable contains the coordinates we’re going to use as the center of our map.
After that, we create the variable settings. You have a lot of options here.
zoom specifies—you guessed it—how far the map will be zoomed in. Play around with the number to get it to fit your location.
center specifies our center. By writing latlng, we refer to the variable we created earlier, and the coordinate inside that will be used.
The last code changes the layout of the map to a bit more minimalistic look in my opinion. The controls in the upper right corner (Map, Satellite, Terrain) are changed to a drop down menu, and the scaling/navigation controls in the left size are changed to small controls.
mapTypeId: google.maps.MapTypeId.ROADMAP defines that our map should be of the type ROADMAP – you can change this to either SATELLITE, HYBRID or TERRAIN.
Below the previous code, write this:
1 | var map = new google.maps.Map(document.getElementById("map_canvas"), settings); |
This code creates the variable map, and defines that the map should use the settings we just created.
Write
1 2 | }
</script> |
to end the function, and move to <body>, and write this:
1 2 3 | <body onload="initialize()"> <div id="map_canvas" style="width:800px; height:500px"></div> </body> |
By doing this we are telling our site to execute the initialize() function when the site is loaded, and insert a <div> with the size we want our map to be.
Try to view your site now. Cool, isn’t it?
Adding Markers
Now we have to add some markers. Let’s start by creating a standard marker—we’ll customize it in a moment.
Right below
1 | var map = new google.maps.Map(document.getElementById("map_canvas"), settings); |
insert the following code:
1 2 3 4 5 6 | var companyPos = new google.maps.LatLng(57.0442, 9.9116); var companyMarker = new google.maps.Marker({ position: companyPos, map: map, title:"Some title" }); |
Try to update your page, and watch the magic. So, what have we done?
First, we create the variable companyPos, where we specify the position of the marker. Next, we create the marker itself using the variable companyMarker. You can add more settings than these, but we will get to that later. These settings are fairly logical, so I won’t go into more depth with them.
Customizing the Markers
Even though this could be enough to show your customer how to find you, we can still make it a lot nicer. Create an image in Photoshop with the size 100×50 pixels, and create something similar to this:
Next, create a shadow for your image:
To add these images as a marker instead of the standard marker, change the marker code to this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | var companyLogo = new google.maps.MarkerImage('images/logo.png', new google.maps.Size(100,50), new google.maps.Point(0,0), new google.maps.Point(50,50) ); var companyShadow = new google.maps.MarkerImage('images/logo_shadow.png', new google.maps.Size(130,50), new google.maps.Point(0,0), new google.maps.Point(65, 50) ); var companyPos = new google.maps.LatLng(57.0442, 9.9116); var companyMarker = new google.maps.Marker({ position: companyPos, map: map, icon: companyLogo, shadow: companyShadow, title:"Company Title" }); |
What we have done here is also really simple. The variable companyImage points to the name of the logo image. Then it defines the size of the image, the origin of the image, and the tip of the image (where the image will be attached to the coordinate). Next, we do the exact same thing for the shadow in the variable companyShadow. In our companyMarker variable we add icon and shadow, and that is basically it.
Now, if you refresh your site, you will se that the marker has changed into your own logo with an added shadow to it as well. To add more markers, you just follow the same method (remember to change the names of the variables).
If you have two markers very close to each other, you might want to add some z-index. The marker with the highest z-index, is the one on top:
1 2 3 4 5 6 7 8 | var companyMarker = new google.maps.Marker({ position: companyPos, map: map, icon: companyImage, shadow: companyShadow, title:"Høgenhaug", zIndex: 4 }); |
Adding Infoboxes
To add a description of your company when the visitor clicks on the logo we can add a infobox. With the Google Maps API it’s peace of cake.
Paste this code right after you define the map variable:
1 2 3 4 5 6 7 8 9 10 11 12 | var contentString = '<div id="content">'+ '<div id="siteNotice">'+ '</div>'+ '<h1 id="firstHeading" class="firstHeading">Høgenhaug</h1>'+ '<div id="bodyContent">'+ '<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>'+ '</div>'+ '</div>'; var infowindow = new google.maps.InfoWindow({ content: contentString }); |
The code here is fairly straight-forward, and you are of course not limited to headlines and paragraphs – there is room for images as well. To make the infobox appear when your logo is clicked, simply add this code right before the last } in the initialize() function:
1 2 3 | google.maps.event.addListener(companyMarker, 'click', function() { infowindow.open(map,companyMarker); }); |
To make the infobox just a little more pretty, add some styles in your stylesheet file:
1 2 3 4 | body { font-family: Helvetica, Arial, sans-serif; font-size:10pt; } |
And there you have it. One piece of fine-looking Google Map to include on your company website, your travel blog, etc.
Download
If you’d like to download the sample files, you can get ‘em right here. Be sure to leave a comment!
Related posts:
162 comments so far
Trackbacks & Pingbacks
- Personalizar mapas de Google | Mailing Factory
- links for 2009-11-20 | HiRes Squad
- Customized Google Maps on Your Website
- Agregando Google Maps a tu sitio web | ubiraci.com
- Chris Robson Design.co.uk » Custom google maps
- Блог-шоу - выпуск 30 - Продвижение сайтов, веб-дизайн и креатив
- Google Maps API - Personaliza tu mapa - Añade tu negocio a GOOGLE MAPS | Smultron POTENCIA !!
- Twitted by n0unours
- Insertar y personalizar Google Maps en tu sitio web - elWebmaster.com
- PixMedial — Design & Geek » Personaliza el Mapa de GoogleMaps
- Implementacija Google maps | T&R splet - internetne storitve
- Friday Fix Oct 12 – 16 | Programming Blog
- Daily Digest for October 21st | Mike Hayes
- Teknologeek.com » Cómo Agregar Mapas Personalizados a tu Sitio Web o Blog
- Resource Roundup | Andy Brewer
- Heti események | I build websites
- Embed Customised Google Maps into Your Website | Programming Blog
- Персонализирана Google Maps във Web 2.0 Приложение | Web 2.0 Blog
- Tutorial per inserire una mappa personalizzata di Google nel nostro sito web
- Notable Tech Posts – 2009.10.18 | The Life of Lew Ayotte
- Adding Google Maps to Your Website | blogfreakz.com
- Google Maps API mini ceļvedis | onkulis.com
- Daily Digest for October 18th | More Than Scratch The Surface
- Embed Customised Google Maps into Your Website | trackteq.com
- AMB Album » Embed Customised Google Maps into Your Website
- Friday Fix Oct 12 – 16 | Master Design
- links for 2009-10-17 « Giri’s Blogmarks
- Adding Custom Google Maps to Your Website | Stiern.com
- Embed Customised Google Maps into Your Website | Internet, Marketing, Enterprenuership
- Cum pun în situl meu o hartă Google | CNET.ro
- Aggiungere una google maps personalizzata nel proprio sito web - Caputo’s blog - Informatica, tecnologia, programmazione, fai da te, papercraft e papertoy
- Friday Fix Oct 12 - 16
- links for 2009-10-16
- links for 2009-10-15 « Boskabout
- The Barcus Family » links for 2009-10-15
- links for 2009-10-15 | Glorified Monkey
- polaris14551 | 摇光 » 在Google地图上标记地点
- links for 2009-10-15 « Uncle Joe's House of Crazy
- Adding Custom Google Maps to Your Website | Stiern.com « gregsmithsays…
- links for 2009-10-14 « Donghai Ma
- TwittLink - Your headlines on Twitter
- Del.icio.us op 14 oktober 2009 | Michel Vuijlsteke's weblog
- Seta Digital Blog » Blog Archive » links for 2009-10-14
- CSS Brigit | Adding Custom Google Maps to Your Website
- Mittwoch, 14.10.09 – Web Tweets | abtwittern
- Bookmarks for October 3rd through October 14th |
- 28 top trending topics in webdesign, technology, development and seach engine optimization « Adrian Zyzik's Weblog
- Adding Custom Google Maps to Your Website | Stiern.com « Netcrema – creme de la social news via digg + delicious + stumpleupon + reddit
- Adding Custom Google Maps to Your Website | Stiern.com » Web Design
- Tweets that mention Adding Custom Google Maps to Your Website | Stiern.com -- Topsy.com











I like this project and I’m attempting to use it. I’ve got things working OK, but I have a large area with lots of buildings. I wanted to do multiple markers with multiple info windows. I made copies of the necessary code, changed the variables and it works, only the info windows won’t go away when someone clicks on the map or another marker. Can someone explain what I should be doing to make that happen?
I can not figure out how to make this popup window contentString displayed on load not on click…. any tips?
BTW. Great article man.
that was wonderfully explained. thanks for the walk-through, you discussed it in every single detail.
Great post and really easy to follow.
I reckon I’ll design some nice markers and replace the standard Google Maps feed with my own one in the very near future!
Thanx for sharing….Really awesome..I ll try in my site..
!!! There is an error message in IE6 and IE7 because of the trailing coma in this code bellow:
var companyMarker = new google.maps.Marker({
position: companyPos,
map: map,
icon: companyLogo,
shadow: companyShadow,
title:”Company Title”,
});
Nice catch, marius! It’s corrected in the code now. Thanks.
Love the blurred map… Makes it look deep.
Thanks, nice new concept.
Thanks man – great post… ^_^
Great post and a useful resource for web developers working with Google Maps. I’ve added the link to my blog here: http://www.andybrewer.com
Nicely explained, Peter…and there are some valuable comments too. Thanks.
This is awesome man! Thanks for sharing it with the rest of us!! God bless..
Thanks a lot! Very usefull …
Here’s a nice webtool for googlemaps coordinates: http://www.vivid-planet.com/sandkiste/google_maps_koordinaten/
That’s so cool! I’m definitely going to use that in the future!
thanks… its so usefull….
Great post.
Nice and simple, good starter read…
Well done. Easy to implement, very informative. I will add it to my aerial photography site. Thx.
Really nice… thanks a lot
Nice and detailed! Thanks.
wow. nice article. thanx a lot.
Does it validate?
If you put all the JavaScript in an external .js file, it should validate.
Nice feature, good post. I’m sure I’ll use this on my pages. Thanks!
For a long,long time I´ve been searching an clear and direct explanation for embebing this code and to get an personal custom map without lossing quality. Al last I found your web through STUMBLE UPON and Eureka . This is it.
THANKS. FOR SHARE YOUR SKILLS WITH US, WITH THIS ACTION WE ARE GETTING AN INTERNET MORE FRIENDLY FOR ALL !
Great explanation man
It’s very usefull
Regards
I have added some code so I can map any address dynamically (for instance when I have more addresses in a database). Maybe it’s not the best coding, but it works for me.
$gresult = file(‘http://maps.google.com/maps/geo?q=ADDRESS&output=csv&oe=utf8&sensor=false&key=GOOGLEKEY’);
list($gm_status,$gm_soort,$gm_lat,$gm_long) = split(“,”,$gresult[0]);
ADDRESS = the address how google wants it in the URL, just like “1+Infinite Liip,+Cupertino,+CA+95014,+USA”. This address will come from your database.
$gm_status is the status code, so you should check if this is 200.
$gm_soort is the the accuracy, so you can decide when to show and not (I have mine set at > 4).
$gm_lat and $gm_long are the lattitude/longitude which you can then use everywhere you need those.
I hope it’s clear what I mean.
I am interested but is not clear enough for me.
You mean that having an ADDRESS on the DB, request to google geocode in order to obtain de status, accuracy, latitude n longitude ($gm_soort,$gm_lat,$gm_long) and then probably Insert them into DB?
Hey man, great article. I’ve already knicked and implemented it on my own site.
Just one thing, for our less adept coders.
You state the variable
var companyLogo = new google.maps.MarkerImage(‘images/logo.png’…
But then call on “icon: companyImage,” which would explain why some people might have some trouble getting their custom icons to display.
Simply change “icon: companyImage,” to “icon: companyLogo,” and you’re all set.
Aarh, I’m so sorry! That one totally slipped. Thanks, Coogan.
Could you provide us with a link to your own site so we can see it?
This is fantastic! Just what I needed!
I’ve implemented and tested with Firefox and Chrome, they work perfectly. However with IE6 (shitty browser, I know) it comes up with the following error:
“Line: 21
Char: 5
Error: ‘google’ is undefined”
when using your sample pages.
Any idea how to fix that?
Do’h! Nevermind… IE was still using proxy settings of my work environment, while I’m bypassing proxy with Firefox and Chrome. Once that was fixed, it works
Silly me…
Ha ha, glad you made it work!
Everything worked except I’m having trouble displaying custom markers. Standard marker appears okay—custom marker doesn’t show at all. Try 174.132.190.8/~valcom99/map_code.txt to see the code.
Thanks,
WittWicky
Hi WittWicky,
When I visit your link, all I get is an empty TXT file…
Thanks for responding.
That was the code relevant to the Google Map. The actual web site is http://174.132.190.8/~valcom99/portfolio.htm. I replaced the code with the sample code and it now works properly, but the text file contains code causing a marker display problem.
WittWicky
Found the problem! It was the misnamed companyImage variable noted in Coogan’s comments shown above. All set now.
Yeah, once again I’m sorry about that.
You could try OpenStreetMap.org. The maps are often better than Google and are truly free to use. The underlying data is available too.
Hi Chris,
Thanks, that looks pretty sweet!
This stuff is getting easier and easier. I still like Google Maps the best though.
what’s the Google Maps API use for?
Educational. Nice to visit sites like yours. I have learned enough today. I will visit you again.
awesome! very informative.
awesome. Thanks very much
Thank you! This is exactly what we were trying to figure out how to do with one of our applications. Then this morning I looked in Google Reader and there was this little RSS feed from the programming Gods.
All kidding aside, this is kick ass and is exactly what we needed to make one of our apps for our clients more user friendly.
Great post. I’ve been looking for a way to spice up and customize my clients’ maps.
Nice work!
Very informative.
Great work !
very very useful and a well written tutorial.
thanks for this.
Yeah! Thanks a Lot! I used G maps API in past but not with teh custom logo.
Great Article … thank’s
very nice tut!
straight to the point and easy to follow, with no fluff
thanks!
Amazing article. Thanks!
I’ve succesfully implemented a directions to here option in the info balloon via this method: http://bit.ly/4DkHm
There are some coding flaw in that script, but it’s not hard to figure out and get it to work AND style it.
Hi there,
That’s pretty cool, but unfortunately it will not work with Google Maps API v3 as it doesn’t support directions yet.
Very nice article.
You should have a “Send article to friend” link or something to send articles from your site to an e-mail address.
Thanks.
Hi Ricardo,
Thanks for your comment – the option is hereby added!
Great post..
Is there a way to instead of popping up the information box, make a “directions to here” link like in google maps? That would be a great feature –
URK
Hi URK,
I am actually not sure about how to do that – it is possible, I just don’t know how to do it, but I’ll definitely look into it – it’s a very exciting idea!
UPDATE: I found this post at the Google Maps API v3 Group, and it states that directions are not yet supported in version 3, but that they will be in a short period on time – and when that happens, I’ll post a tutorial on Stiern.com on how to implement it in our map!
Great Tutorial!
Is there a way to replace the map and add your own image to the viewport?
Thanks
JT
Do you mean to use a custom map?
I have seen it done before on maps for things like amusement parks (sorry, I can’t remember which one). Custom maps would be a great addition.
That sounds really cool. I’ll look into it!
Hi, excellent post, very useful thanks.
I have a question for you: How can show track in map? or add a kml file?
Regards.
Hi Robert, and thanks for your comment!
With ‘track’ I assume you mean a ‘route’ – so you’re able to show your customers which way to go, right? I will return with another tutorial on this very soon!
Regarding KML files from Google Earth – I’ll have to look into that, but I’ll let you know!
Useful post. thanks!