DyingLoveGrape.

(home) (about) (rss)

Part 4, Section 1:
The Google Maps API.





Working with the URL.

(Last Update: .)

What is Google Maps?

Google Maps will allow us to look up and plot various locations on an online map. There's tons and tons of uses for it, but we'll stick to some basic things for this tutorial.

For this part, we will take a short stroll through working with the URL — that is, we will be specifying arguments at the end of the URL which will cause google maps to do certain things when the page loads. The next part will go a bit more into how to embed google maps into our own webpages and customize them to display data.

Using Python for Web-Things.

Because Google Maps runs in a browser we will need to learn a few things about how Python works with URLs and browser stuff. So let's just go through that first and we'll figure out a bit later how to use this to look at some google maps things.

As usual, it's almost too easy to get Python to do things we want. Open up Spyder. We'll work with a file this time, so go to File > New File and open up a new file. Instead of importing all of our numpy, scipy, and pandas things, we'll instead import webbrowser:

import webbrowser as wb

And, at this point, it will be rather nice if we could open a URL in a browser for the user. To open the default browser to some url, we use the command

wb.open(your_url)

So, for example, to open up http://www.google.com we would write

wb.open("http://www.google.com")

Make sure you try this yourself to see if it works. If it does, fantastic! If not, then try to follow the errors to see what went wrong; note, for example, that you must put in your website as a string when putting it into wb.open().

That's it. Python makes things pretty easy.

Doing Things with the Map.

The way that google maps allows us to link certain maps to others is to make it visible in the URL. For example, if I want to show you the directions from my house to the closest Pizza Hut, it will give me a huge, long link to send to you so that when you open that link it will show you those same directions. The way this is done is by having an "opening part" to the URL which tells the browser it's going to google maps and an "arguments" part which tells google exactly what it needs to do (get directions, look up locations, and so forth). This opening part will always be the same for google maps and we will only worry about changing the arguments part to get things we'd like from google maps.

Let's code this up. The opening part for google maps is always

opening_part = "https://maps.google.com/maps?"

Which is just the url with maps? at the end of it. Note that you can call this whatever you'd like; I call it opening_part just because it is the opening part of the URL.

Next, we'd like to actually be able to look at something. The way that we can look at a coordinate of longitude and latitude is to specify the value

q=41.949405,-87.632321

directly after the ? in the opening part. The way we'll do this in Python is by saving these to variables and then concatinating the strings. So far, your program should look like:

import webbrowser as wb

opening_part = "https://maps.google.com/maps?"
argument = "q=41.949405,-87.632321"

wb.open(opening_part + place)

Test this out! It should bring you to this value which is in the water close to the Belmont Harbor Dog Beach and some Tennis courts in Chicago.

As a second neat thing, we'll look at directions from one place to another. Let's take two locations: the CTA Blue Line Stop at Clinton [a train stop in Chicago], which is at (41.875539, -87.640984), and the CTA Adam and Wells stop [also a train stop in Chicago], which is at (41.879426, -87.634024). Here, we're going to need two arguments: a starting place and a destination. The way to concatinate arguments is via an & like this:

https://maps.google.com/maps?argument1&argument2&argument3

Instead of using q= like we did above for a point, the way to make a starting address argument is:

saddr=41.875539, -87.640984

Note that Google Maps is powerful enough that we can actually specify place names instead of coordinates but, for now, let's stick to coordinates. The important part is that the starting address argument is saddr= . Similarly, the destination address argument is daddr= .

This is good news. It means we can enter our starting and destination coordinates as follows:

opening_part = "https://maps.google.com/maps?"
argument_1 = "saddr=41.875539, -87.640984"
argument_2 = "daddr=41.879426, -87.634024"
amp = "&"

wb.open(opening_part + argument_1 + amp + argument_2)

What else is there?

For the most part, the way I figured out what arguments did what was looking at the links that google maps gives you to share maps with others and checked out which of the arguments were relevant and which others were just extra ones (usually, there's a ton of arguments at the end which aren't entirely necessary; delete a few and see what (if anything) changes). This type of thing — putting arguments after the standard URL — can be done on many different sites: youtube, google, google maps, twitter, tumblr,...

While this is fun and all, it would not be useful to us if, say, we had our own website that we wanted to have a map on. For example, we might have a website that shows different crime rates for different areas and we would want our users to be able to see this data without having to click on a link and go all the way over to google maps; we should be able to embed a map in a website and we should be able to customize it.

In the next part...

Of course, this is totally possible to do! To do it, we need to (alas!) work with the JavaScript. Luckily, we won't need to learn much JavaScript to be able to work with it &mdash it will suffice just to be able to understand what some of the code is doing. We look into this in the next part.


⇐ Back to 3.2HomeOnwards to 4.2 ⇒