Have you seen an application or software that uses another companyâs service? You might have wondered how the developers managed to get their hands on such a service. Sometimes, however, itâs not nearly as complicated as you may think; it might be a case of the developer using the companyâs API.
API stands for âapplication programming interfaceâ, and it allows developers to use the services a company has created in a safe and restricted manner. Not all services have an API you can use, but youâll be surprised at what you can do with the free APIs available to you. In this article, weâll make a piece of software that talks with Google Maps and creates a static map of a set location.
First of all, weâre going to need something to put our map in. For the sakes of this article, Iâm using Visual Studio Community 2015, which is free to download and contains everything you need. If you have your own coding language which you can follow along with, feel free to use it!
To start, weâre going to make a Windows Form. This allows us to make fields and buttons easily, as well as being able to embed the map we want within its browser functionality. Go ahead and make a new Windows Form by clicking New->Project, then Visual C#, then clicking Windows. On the right, choose Windows Form Application.

Youâll see a blank UI window appear in the project. Thatâs the window weâre going to use to build our map program on. This is what makes Windows Forms very useful, as you can create a quick program that doesnât require much coding to create its UI.
This form we see here is called Form1, which weâll rename to MapControl. Eventually, weâre going to use it as a launching point for our map. For now, simply create a button called âShow Mapâ. You can do this by finding Button on the toolbox and dragging it onto the form. If you donât see the toolbox, click View, then Toolbox.

We want this button to create a popup showing our map. Before we can code the button, however, we actually need a second form to open! Right-click your project on the right-side and go to Add->Windows Form.

Call this form something memorable (it will be called âMapDisplayâ in this article) and click OK.
Now we can make the button open our new form. Double-click the button on the form itself and Visual Studio will automatically make a function called â[button name]_Clickâ and show you it. This code will activate when the button is clicked. Now, input the code into this function that opens MapDisplay, like this:
var MapWindow = new MapDisplay();
MapWindow.Show();
So now the second window will open when we click the button, but we still have no map. To show one, we need to talk to Google to let them know weâre going to be using their map service, and to do that, we need an API key.
Head over to the Google Static Map API webpage here: https://developers.google.com/maps/documentation/static-maps/. At time of writing, you can use Googleâs static map API at 25,000 times a day or in a commercial way before you have to pay them money, so donât worry about having to buy anything! Click the âGet APIâ button, then give your project a name.
What this will do is give you a really long string of random letters and numbers. This is your personal API key, and what youâll be using to get your map from Google. It basically lets Google know that youâre making a request from their maps server, so they know whoâs using it You can find all your keys again at https://console.developers.google.com/apis/credentials if you lose your key, so donât worry!
Now that we have our key, we can query Google for a map by using custom URLs. We build a URL that contains all the information we want from Google, add our key in there to identify ourselves, then send it to Google. Google then gives us the information, and we can show it within our application.
So, letâs build a URL. First, we need to tell Google we want a static map. To do this, our URL must start with:
https://maps.googleapis.com/maps/api/staticmap
This tells Googleâs API system that we want to use a static map. If you try to visit this URL by itself, itâll give you an error message, because it needs a little more information before it can give you a map.
For one, it needs a Size. Letâs go ahead and add a size to this URL. We add parameters by adding an & to the url, then the parameter name, then an equals sign, then the value of the parameter. For example, if we want a map at size 400×400, we add this to the end of the URL:
&size=400×400
We can use the Center parameter to tell the map to lock onto a specific place. For this example, weâll lock onto Tokyo, so weâll add â¢er=Tokyoâ to the URL.
We can also tweak the zoom level of the camera using the Zoom parameter. This zoom ranges from 1 to 20, and Google recommends the following values:
1: World
5: Landmass/continent
10: City
15: Streets
20: Buildings
For this example, weâll add â&zoom=10â to our URL.
Finally, letâs toggle how the map appears using the MapType parameter. We can feed it several values; roadmap for the default option, satellite for a photographic image, terrain to show the geographical elements of the map, and hybrid for a mix of the roadmap and the satellite images. For the sakes of this example, weâll add â&maptype=satelliteâ to the URL.
To top it off, we add our API key we got earlier by using &key=[Key]. Now weâre good to go!
You should now have a URL that looks something like this:
https://maps.googleapis.com/maps/api/staticmap?&size=400×400¢er=Tokyo&zoom=10&maptype=satellite&key=[YOUR KEY]
Letâs go back to our MapDisplay form. Add a WebBrowser element to it (called âMapBrowserâ in this example) by clicking and dragging it onto the form. Press F7, and youâll see the formâs code. Type in the following code within the âpublic MapDisplayâ function:
String url = âhttps://maps.googleapis.com/maps/api/staticmap?&size=400x400¢er=Tokyo&zoom=10&maptype=satellite&key=[YOUR KEY]â MapBrowser.Navigate(new Uri(url));
Press the F5 key, or the Start button at the top, to start the program. Now when you click the button on the form, it should open up a window with your map in it!
Where To Go Next
Flesh Out MapControl
Our MapControl can open the map, but we have to go into the code every time we want to change the URL, which is a huge hassle! Using Windows Forms controls, set it up so you can enter the mapâs variables in MapControl (TextBox and ListBox controls will be useful here, as well as Labels to label them). Then, pass them to the MapDisplay, which then uses the data to construct the URL and direct the web browser to it.
Use The Google Maps Javascript API
If youâre feeling particularly brave, you can make Google Maps using its Javascript API, which can be customised for zooming, scrolling, and even markers. You can find a link to the API in the links section, but be warned; it should only be tackled by advanced programmers!
Learn More
Source Code
Visual Studio Community
https://www.visualstudio.com/free-developer-offers/
Google Static Maps API
https://developers.google.com/maps/documentation/static-maps/
https://developers.google.com/maps/documentation/javascript/
Google Javascript Maps API
Windows Forms help
https://msdn.microsoft.com/en-us/library/ms229601(v=vs.110).aspx