How to use the Google Maps API
videos

This is How You Use the Google Maps API – screencast

So your client emails you and asks, “Can you put one of those flashy maps on my contact page so that users can actually see our building from a satellite’s view?”. You’ve used Google maps many times, but there is only one problem: you have no idea how to use the API. Well get out your spoon and dig in!

* Click on the full-screen toggle.

Step 1: Obtain a Unique API Key

If you were to download the source code that is provided with this article, you would find that it doesn’t work on your website. The reason is because Google requires all users to obtain a unique “API key” for each site that implements Google maps.

Never fear. It’s 100% free and takes about thirty seconds to sign up. First, visit Google’s sign up page and enter the url of your website. Don’t worry about adding a specific path. The root url will cover every page that is part of that domain. Agree to the terms and conditions and click “Generate API”.

Generate Key

That’s it! The page that you’ve been redirected to contains your unique key as well as a sample page – to serve as a crash course. Your key will look something like:

ABQIAAAAAq93o5nn5Q3TYaaSmVsHhR1DJfR2IAi0TSZmrrsgSOYoGgsxBSG2a3MNFcUDaRPk6tAEpdWI5Odv

You’ll also find a script path that will look like:

<script src="http://maps.google.com/maps?file=api&v=2ampkey=ABQIAAAAAq93o5nn5Q3TYaaSmVsHhR1DJfR2IAi0TSZmrrsgSOYoGgsxBSG2a3MNFcUDaRPk6tAEpdI5Odvw" type="text/javascript"></script>

Yours will be different from mine because it will contain your own specific key value. Copy this and paste it into the head portion of your document.

Sign Up Complete

You might want to bookmark the link to the API documentation. You’ll undoubtedly be referencing it as your skills progress.

Step 2: Setting Up Your HTML

For the sake of simplicity, we’ll create a bare bones layout. Add the following within the body element of your document.

In a real world situation, you should move the styling to an external file (like I did in the video). The height and width values will by used by the API to determine the dimensions of your map. Don’t worry, nothing will be clipped off.

Step 3: Javascript

Next, add the following to your Javascript file. Review it a bit and then continue on.

$(function() { // when the document is ready to be manipulated.
  if (GBrowserIsCompatible()) { // if the browser is compatible with Google Map's
    var map = document.getElementById("myMap"); // Get div element
    var m = new GMap2(map); // new instance of the GMap2 class and pass in our div location.
    m.setCenter(new GLatLng(36.158887, -86.782056), 13); // pass in latitude, longitude, and zoom level.
  }
else {alert("Your browser is not worthy.");}
});

To take this code line by line:

  • When the document is ready to be manipulated, run the code within.This is a jQuery syntax, but jQuery isn’t required in the least. You could also simply add an “onLoad()” attribute to your body element – though this is messy.
  • If the browser that the user is accessing the map from isn’t compatible with the API, then show an alert (see bottom). Otherwise, run the code within.
  • Create a variable called “map” and tell it to find the div that will contain the map.
  • Next, create a variable called “m” and make it equal to a new instance of the “GMap2″ class. Within the parenthesis, pass in the “map” variable that you just created previously
  • Finally, set a center point so that the map knows what to show. To do this, we create a new instance of the “GLatLng” class and pass in the latitude and longitude values. You can go here to grab the appropriate values. In my case, I’ve set the coordinates to my home town. After that, you can optionally input a zoom level – which I’ve set to the standard ’13′.

This code alone will give you a basic map that might be completely appropriate for your needs. However, if you’d like to also implement “zoom” and “map mode” features, read on.

Step 4: Refining Our Map

There are literally dozens of features that you can add to your map. We’ll go over a few of them. First, we’ll implement a zoom bar that will allows the users to incrementally zoom in or out.

Layout
m.addControl(new GLargeMapControl())

Here, we’re taking our map and are adding a new control called “GLargeMapControl”.

Next, let’s add a feature that will allow the users to choose which map mode they desire – Normal, Satellite View, or a hybrid.

var c = new GMapTypeControl(); // switch map modes
m.addControl(c);
  • Create a variable called “c” and make it equal to a new instance of the “GMapTypeControl” class.
  • Add a new control, and pass in “c”.

If you refresh your browser, you’ll see that the user nows has the option to choose his viewing mode. But what if you want to set the default mode? In such instances, you would use “setMapType”.

m.setMapType(G_SATELLITE_MAP);

When defining the default mode, you have three choices.

  • G_SATELLITE_MAP
  • G_NORMAL_MAP
  • G_HYBRID_MAP

You’re Finished!

That wasn’t too hard, was it? There are a few specific class names that you’ll need to memorize, or jot down for later reference. But other than that, it’s strikingly simple to implement such an advanced map into your websites.

For you template designers, why not implement this into one of the themes that you sell on ThemeForest?

A few days ago, we posted a tutorial that shows you how to combine many APIs – including Google maps. But many of you didn’t know enough to get started. Hopefully, this will help. After you’ve wrapped your head around this API, why not save yourself some headaches and enlist the help of a PHP class called Phoogle? I think it’s best to learn the right way first, but now that you have…cut some corners! See you next week.

Additional Resource

  • Create A Mashup By Combine 3 APIs

    How to Create a Mashup By Combining 3 Different APIs

    This tutorial will show you how to create a mashup of three different APIs including integration with Google Maps.

    Visit Article

    • Subscribe to the RSS Feed and the Video Feed for more daily web development tuts and articles.


Tags: Videos
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Peter

    Ahhhh Finally I found this your post as a true and easy tutorial about Google map integration..

    Thanks n keep it up!!!

  • Brian

    Hey, Jeffery – Excellent basic lesson on GMap API. How about a sequel which shows how to use your own custom map and the new version 3 API??

    I have a task which would be perfect for the Google API, however it envolves a [custom] campus map. I’ve heard this is possible, but not sure how to go about it.

  • Ru

    Hi the screen cast was very helpful thanks, reading through Google i was getting overwhelmed with the API Reference Guide and couldn’t visualize how each class is used to come up with a simple map like yours!

    How i insert the little pointer that one can click on to view the bubble message?

    Or is this screen cast coming in the future?

  • Angelos Kar

    Is it possible to embed two maps on the same page? I tried but I didn’t manage to accomplish it…

  • Pingback: Tutorial Google Maps API | iniGIS.info | GIS Blog Indonesia

  • Chris

    that’s fantastic n helpful!
    just wondering is any possible to know whose closer to me? like say, i login to my website n can know who’s member is closer to me rite now(noline together in real time)?

    thx you in advance.

  • Lori Jeske

    i’d like to display a Google API Map that references a map from my network… any suggestions?

  • http://www.luutaa.com Achint Verma

    simple yet effective ….. thanks

  • Sandeep

    Hi Jeffrey,
    Cool quick start.
    Could u please share how to integrate google map with Standalone swing application ?

  • http://www.selbstklebefolien.com/ Klebefolie

    Great source how to use the Google Maps API Best regards

  • http://www.soysanluis.com Marcos Alduenda

    I had not been able to find a video tutorial on this, I was begining to wonder if people care about google maps. Thanks you, you make it very easy to understand.

  • http://www.mywandtattoo.de/ Klebefolie

    Frohes neues Jahr 2010 wünscht Euch Maike

  • http://www.wandaufkleber.com Wanddekoration

    Nice tutorial, very useful I hope I will handle it with this informations THX

  • http://sidouglas.net Simon

    Your demo 404s!!

  • http://www.tikoim.de Tikoim

    Nice tutorial – bit advanced but it works
    @Simon the demo site -jeff-way com- is for sale – PR3, not bad
    cheers

  • http://www.tikoim.de Tikoim

    Great tutorial – bit advanced but it works
    @Simon the demo site -jeff-way com- is for sale – PR3, not bad
    cheers

  • http://www.daveworks.net Dave Naves

    thanks so much for taking the time… really appreciate your efforts, jeffrey.

    cheers,
    //dave

  • http://www.daveworks.net Dave Naves

    nice! thanks for taking the time, jefferey.

    cheers,
    //dave

  • http://www.adriancrellin.co.uk Adrian

    Hi, Thanks for giving me a bit more of an understanding of google maps API.

    I was just wondering if anyone had any thoughts on something. I am a freelance designer and like to offer clients the opportunity of having a google map, showing their business, on their websites. Do you think I am right to ask them to add their business to google maps first themselves, rather than me adding a google business listing to google maps myself for them?

    Any Thoughts?

  • chris

    Thanx ;-)

    Got the google maps API working at last!

  • http://www.ipadschutzfolie.de Tasche

    This works ! ! ! very nice tutorial. You need the jquery, tried to remove it this does not work.
    One question, what tools did you use in your demo ? what elearning software is that ?
    Thanks

    Ipadschutzfolie

  • http://www.nunomedia.com Nuno

    Hi guys,

    I have a problem, I followed this tutorial and it worked, thanks. But when I embed everything in a existing html it disable the drag functionality. I can not drag in the map. Help please.

  • Renee

    The code for the tutorial worked great in chrome and safari, but in opera, ie and firefox, I don’t get is the actual map image just the left most navigation and right selections, but no map image. What do I need to change to make the map image show up in these browsers?

  • Elenore

    The DEMO site no longer works because the domain name is for sale!!

  • http://www.feuerwehr-spiele.com Feuerwehr Spiele

    Very nice tutorial. It works perfect :-) thank you

  • http://www.mywandtattoo.de/ Wandtattoo

    Great Content. Best regards Maike

  • alkis

    Thanks, really helpful to get started.

  • asha

    its really a good website and i ts really help me to do my final year project as well..
    please help me more if i have any questions about the Google map…
    thanx

  • http://www.finanznachrichten.de Aktien

    I have been doing some ‘research’ how to work with Google Maps and an API … and I landed here. Thank you for your good support info. Better than Google’s FAQs!

  • http://www.agri.web.tr/ agri dagi

    Got the google maps API working at last!

  • http://www.q1q.de fahrzeugortung

    Hi the screen cast was very helpful thanks, reading through Google i was getting overwhelmed with the API Reference Guide and couldn’t visualize how each class is used to come up with a simple map like yours!

  • http://www.wazzam.in Arun

    Really helpful ………!

  • Pingback: This is How You Use the Google Maps API – screencast « Chandara

  • David

    Nice Tutorial, really helpful

  • http://www.webdesign-luene.de/hansedesign.htm Web Design

    brilliant tutorial it has already moved once and functions first-class

  • http://www.journeytopeak.com Jia Jun

    So can we integrate it so users can submit their information into our google maps?
    for example like this: http://cityofsharedstorieskualalumpur.com/ or this : http://www.streetartview.com/
    I’m working up on such project but don’t have much basic on java, html or flash. Afraid I can’t make it, seems quite complicate for my current level to make it.

  • http://www.avrasyasoft.com Alparslan

    Good…
    Thankyou…

  • Pingback: FYP 2 !!!!!!!! « JJ FYP 2010/2011

  • http://www.vakuumtuete.com Vakuumtuete

    very good tutorial. thanks for writing and sharing.

  • http://www.kaffeemaschinen-reparatur.de Angelo

    ’m somewhat certain they’ll be informed a lot of new stuff right here than any one else!

  • http://www.deine-wandtattoos.de/ Tim

    Many thanks for this so very useful Tutorial!

  • http://americanpregnancy.org/forums/member.php?u=131580 gadzet

    I’m also commenting to make you know of the useful experience my cousin’s daughter obtained using your blog. She mastered plenty of things, including what it’s like to possess a marvelous teaching nature to get men and women without difficulty thoroughly grasp selected advanced subject areas. You really exceeded our own expectations. I appreciate you for offering these warm and helpful, trusted, edifying as well as fun guidance on that topic to Tanya.

  • http://www.handy-online-verkaufen.de/ Handy verkaufen

    Very very thanks for this Post!

  • http://stickerdesign24.de Wandtattoo

    Very nice and informative post,
    thanks a lot.

    Best regards from Germany

  • http://www.lkwspiele.eu LKW Spiele

    Really great source how to use the Google Maps API

    Best regards

  • http://www.colmar-elsass.de/ Sascha

    Thanks for the tutorial. The Google Maps api is a great feature – and it works :-)

  • http://www.richtig-schoen-kochen.de Küchenzubehör

    Thx a lot!

  • Tazeen

    Awesome Post!! helped me a lot! Thanx!

  • SirElroyDaxter

    How cute! Jeffrey didn’t discover Sublime Text yet!!