Using the Google Maps API to get long/lat co-ordinates from a postcode or address

Storm were recently asked to create a local office finder for franchised business VoIP provider, Datasharp. The client requested that the user should be able to enter their postcode or town/city and be informed where their nearest branch was.  To make this possible we would need the latitude and longitude co-ordinates for each office and the customer, but how do you get the co-ordinates from the address?

Using the Google Maps API

Note: To make use of the example in the post you will need to sign-up for a free Google Maps API Key.

The following example is written in C#, but it can be converted to any language fairly trivially as we are actually only making an HTTP request and parsing the result.

We use a little helper class to hold a latitude/longitude co-ordinate.

public class LatLng
{
    public double Latitude {get;set;}
    public double Longitude {get;set;

    public LatLng(){
    }

    public LatLng(double lat, double lng)
    {
        this.Latitude = lat;
        this.Longitude = lng;
    }
}

We’ve packaged the function to get the co-ordinates in a helper class so that we can add extra Google Maps functionality if required. It also allows us to reuse the code across multiple projects as the API key is not hard coded.

    using System;
    using System.IO;
    using System.Net;
    using System.Web;
    using Storm.Common.Utils.Extensions;

    public class GoogleMaps
    {
        private string API_KEY = string.Empty;

        public GoogleMaps(string api_key)
        {
            this.API_KEY = api_key;
        }

        public void SetApiKey(string key)
        {
            if (key.IsNullOrEmpty())
            {
                throw new ArgumentException("API Key is invalid");
            }

            this.API_KEY = key;
        }

        /// <summary>
        /// Perform a geocode lookup of an address
        /// </summary>
        /// <param name="addr">The address in CSV form line1, line2, postcode</param>
        /// <param name="output">CSV or XML</param>
        /// <returns>LatLng object</returns>
        public LatLng GetLatLng(string addr)
        {
            var url = "http://maps.google.co.uk/maps/geo?output=csv&key=" +
                       this.API_KEY + "&q=" + HttpContext.Current.Server.UrlEncode(addr);

            var request = WebRequest.Create(url);
            var response = (HttpWebResponse)request.GetResponse();

            if (response.StatusCode == HttpStatusCode.OK)
            {

                var ms = new MemoryStream();
                var responseStream = response.GetResponseStream();

                var buffer = new Byte[2048];
                int count = responseStream.Read(buffer, 0, buffer.Length);

                while (count > 0)
                {
                    ms.Write(buffer, 0, count);
                    count = responseStream.Read(buffer, 0, buffer.Length);
                }

                responseStream.Close();
                ms.Close();

                var responseBytes = ms.ToArray();
                var encoding = new System.Text.ASCIIEncoding();

                var coords = encoding.GetString(responseBytes);
                var parts = coords.Split(",");

                return new LatLng(
                              Convert.ToDouble(parts[2]),
                              Convert.ToDouble(parts[3]));
            }

            return null;
        }
    }

Let’s talk through whats going on. First up, the boring bit:

        private string API_KEY = string.Empty;

        public GoogleMaps(string api_key)
        {
            this.API_KEY = api_key;
        }

        public void SetApiKey(string key)
        {
            if (key.IsNullOrEmpty())
            {
                throw new ArgumentException("API Key is invalid");
            }

            this.API_KEY = key;
        }

Here we have a private field to store the Maps API key required to issue a request. The key can be set either through the constructor or by using the SetApiKey() function.

Now lets see how we use this in the lookup function.

            var url = "http://maps.google.co.uk/maps/geo?output=csv&key=" +
                       this.API_KEY + "&q=" + HttpContext.Current.Server.UrlEncode(addr);

            var request = WebRequest.Create(url);
            var response = (HttpWebResponse)request.GetResponse();

First, we build the a URL which represents the request we want to make. The output param sets the format Google will return the results, here we have chosen CSV but you can also get JSON if you wish. Next we include our API key to identify ourselves. Finally, we pass the address we are searching for.

Using this URL we create a WebRequest and call GetResponse to fetch the page contents. Before we go any further we check that the result was returned successfully:

if (response.StatusCode == HttpStatusCode.OK)

Now we have another boring bit which reads the result of the web request into a byte array and converts the result into an ASCII encoded string

                var ms = new MemoryStream();
                var responseStream = response.GetResponseStream();

                var buffer = new Byte[2048];
                int count = responseStream.Read(buffer, 0, buffer.Length);

                while (count > 0)
                {
                    ms.Write(buffer, 0, count);
                    count = responseStream.Read(buffer, 0, buffer.Length);
                }

                responseStream.Close();
                ms.Close();

                var responseBytes = ms.ToArray();
                var encoding = new System.Text.ASCIIEncoding();

                var coords = encoding.GetString(responseBytes);

This string should look something like:

200,5,51.2268219,-2.4646913

We are interested in the 3rd and 4th values – the latitude and longitude. So, we split the string at the comma separator, create a new LatLng object and return the result:

        var parts = coords.Split(",");
        return new LatLng(Convert.ToDouble(parts[2]), Convert.ToDouble(parts[3]));

I hope this tutorial has proven useful and that you can go away and develop some cool apps!

By Adam Pope

Adam is our tech God, and it's not just his name that's holy - his knowledge of code borders on the supernatural, and whilst he doesn't like to brag, he finished top of his class at university. (Yep, the very top).

When he isn't crafting masterpieces on the web he is knocking them up in the kitchen, his legendary curry being an office mainstay here at Storm.

Adam was recently named as one of South West Business Insider's 42 under 42 for 2012.

stormconsultancy.co.uk →

12 comments

  1. If you are not using the results on a Google map, this is illegal! Found this – “Note: the geocoding service may only be used in conjunction with a Google map; geocoding results without displaying them on a map is prohibited. For complete details on allowed usage, consult the Maps API Terms of Service License Restrictions.” on http://code.google.com/apis/maps/documentation/geocoding/

  2. Hey Bart – we are displaying the results on a Google Map. Once we’ve decided which office is closest we zoom to the map to that location and show their contact details in an info bubble. But thanks for the heads up for future use!

  3. Hi,

    I am working on asp.net webapplication, I need to get google maps from UK postal code and also I have to find out the distance between two postal code addresses.

    Please help me on this.

    Thanks in advance.

  4. when i will give the postal code (zip code ) then i want to get related latitude and longitude .
    how ? could u plz help me?

  5. When i run this project i didnt get result which i should expect but i get always latitude and longitude value 0 in all the zipcode and address. Please can u help me for this ?

  6. is it possible to send iata code and get the latitude and longitude ? could someone help me please.

  7. Hihi, I used ur code but got the following in return :

    The remote server returned an error: (403) Forbidden.

    We’re sorry…

    … but your computer or network may be sending automated queries. To protect our users, we can’t process your request right now.

    Is there any problem with my side?