scriptygoddess

23 Mar, 2007

Simple Google Maps (for dummies)

Posted by: Jennifer In: Bookmarks|Javascript

I was recently asked to embed a map into a web site. Basically, the client wanted a map, with a marker, indicating where their business was, and an easy way for the user to get directions to where they were located.

The first thing you need to do is get a Google Map API key. Just a little warning – once you sign up for the API key – it will show up on the top of the page – and after you leave that page, I have yet to find where you can go to retrieve it again. So copy and paste that key in a safe place! LOL!

Here are a couple of resources that helped me out:
Of course there's the Google Maps API documentation.
Also, this was a pretty good tutorial.

In order to place a marker on the map, you need to know the longitude and latitude of your location. Originally I found "Bud's Blog, which linked to geocoder.us (US locations only) , but for some reason, it couldn't find the address I wanted to mark. I also found this this site which will give you the longitude and latitude (and that one DID find the location). Another source is TerraServer. (US locations only) Here's another one that seemed pretty good. Also, here's another one that will let you click on the map to place a marker – and then if you click on the marker it will give you the lattitude and longitude. (This was particularly useful, because for some reason the address, Google, Mapquest, and Yahoo maps all don't seem to know where the location of this particular client was). (Although you can also kind of do this with Yahoo maps by clicking on an area and then right click in the spot and selecting "drive to here" – on the left side it will give you the long./lat. location of the spot you clicked…)

You can also pull that information from the URL you get from Google Maps.
For example..
If you look up the Red Butte Gardens in Utah, (that place is GORGEOUS by the way!), and click the button to "link to this page", it will give you this URL. If you break down the pieces of that URL, you will see that one part of it looks like this:
ll=40.771182,-111.809063 (not the "sll" value, the "ll" value) That is the longitude and latitude.

I should note that there is some variances/weirdnesses depending on the source you use. If I type in the address in Google Maps, the marker looks like it is where I want it, and if I grab that "ll" value longitude and latitude and use that value on my own map – it shows the marker quite a bit off the road. (Probably more accurate to where the main building is, after their long driveway, but I want to show the location of where their driveway entrance is.) TerraServer was more accurate in that regard.

UPDATE 6/9/07: Just got this link to Tech-recipes off Lifehacker: Once your location in in the center of the map, enter the following Javascript in the address bar. This will pop up a little "alert" type window with the coordinates:javascript:void(prompt('',gApplication.getMap().getCenter()));

(FYI – you can right click on an area of the map and select "center map here" from the little menu you get)

Moving along to creating the map…
When I first started working with the map, I was using one of the samples from "Buds Blog" – however, the sample you get when you first sign up for your API key includes some code that is necessary so that IE doesn't choke. If you've already stripped that info out, and IE is choking, here is a post I found that will explain how to fix it.

Anyway, use the base-starter sample you get when you sign up for the API key and you'll be fine. :)

Their example had one line for setting the point and setting the point at the center, but I needed to separate it into two statements. (As well, I needed to change the long. and lat. coordinates for my location.

So this:
map.setCenter(new GLatLng(37.4419, -122.1419), 13);
Became this:
var gardenPoint = new GLatLng(40.7628,-111.8246);
map.setCenter(gardenPoint, 15)

This sets a point and puts it in the center of the map. The "15" is the "zoom" level. (The higher the number the closer the zoom). Another trick with this is that if you play around with a map directly on Google Maps – do the "Link to this page" thing – look at the URL, and look for the value of "z"… that is the number of what your zoom is.

map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
This adds the typical controls to the map. If you would prefer smaller controls, use this:map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());

var gardenMarker = new GMarker(gardenPoint);
map.addOverlay(gardenMarker);
This creates a new marker at that point, and puts it on the map. (FYI "gardenPoint" and "gardenMarker" are just variable names I made. You can actually make then anything you want.)

var gardenHtml = '<b>Red Butte Botanical Garden</b>,<br /><a href="http://www.redbuttegarden.org/">They have beautiful flowers</a><br />300 Wakara Way<br />Salt Lake City, Utah 84108<br /><br /><a href="http://maps.google.com/maps?f=d&hl=en&daddr=300+Wakara+Way,+Salt+Lake+City,+UT+84108">Get Directions to here</a>';
That makes one of those little "bubbles" and you can pretty much put anything you want to in there. I even put a link to Google maps to get directions with this address as the ending destination.

gardenMarker.openInfoWindowHtml(gardenHtml);This connects that buttle to the marker and makes it so that it's open when the map loads.

Here's the complete example.

I know that's pretty basic – but should I ever need to do that again, I wanted to save all the links and information I needed to create the same map. :)

11 Responses to "Simple Google Maps (for dummies)"

1 | Darrel

March 25th, 2007 at 5:50 pm

Avatar

Thank you so much! Yours is the only example I browsed out of nearly 10 that actually worked. Praise Be to the Goddess!

2 | Great

April 15th, 2007 at 5:16 pm

Avatar

Same as Darrel says.

I was using yourgmaps because of how the other examples wouldn't work for me.

This one explains it great.

Thanks

3 | Serge K. Keller

May 3rd, 2007 at 1:25 am

Avatar

I wouldn't put my hand on a flame on this, but I think that the Google Maps API key is tied with the url you gave in the form. So, if you ask for the API key again with the same uri, you should get the same key.

4 | tek

June 20th, 2007 at 8:37 am

Avatar

is the Google API key valid on a directory level or domain/subdomain level ?

5 | Jennifer

June 20th, 2007 at 8:47 am

Avatar

The way I understand it is that it is valid for a URL/(directory). A subdomain would be considered another URL. However, subdirectories in the same domain/direcotry are valid. So (again, this is my understanding, I could be wrong) this is all valid:
if you registered the key for http://www.mysite.com
this is also valid:
http://www.mysite.com/mymaps/

This would need a new key:
http://mymaps.mysite.com/

6 | Matt Cushing

July 2nd, 2007 at 6:37 pm

Avatar

Is it possible to use the API with addresses rather than lat/long keys? I've been thinking about putting together a map interface for my company that allows people to put a few addresses on a map and see how it plots out.

7 | Simon de Groot

November 25th, 2007 at 9:03 pm

Avatar

Thanks very much Goddess, a nice clean simple example without all the added baggage.

8 | WIlliam Hayes

October 14th, 2008 at 1:50 pm

Avatar

Wayhay! Thanks! I am a complete code novice and have spent all evening wading my way through the google help files getting more and more lost and then your page turned up and now my page works!

People like you make the web special!

Thanks!

9 | Wendy

January 4th, 2009 at 6:02 pm

Avatar

I love you scriptygoddess! This was exactly what I needed to build a map quickly w/o wading through tons of not very good documentation. I can't tell you how helpful this is, I think google should hire you to write their documentation, cause they need some help! You rock.
Thanks so much,
Wendy

10 | Celshader

April 29th, 2009 at 10:51 pm

Avatar

So good to see a nice simple easy to follow example of a very common google map usage.
All the demo's on the google site seem to be written with more advanced users in mind.

11 | mflawson

October 15th, 2009 at 1:33 am

Avatar

Just what was needed! This type of walkthrough is exactly what the web is all about – good clean code offered in readable English (see Celshaders comments).

Well done!

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements