Find out the Geo-coordinates of an address – geocoding with Google Maps

image1375-570x194_thumb.png

 

To find out the exact coordinates of an address there are numerous services available. Some ways goes through Javascript and the Google Maps “Plugin” others are reachable via a surface. The “smartest” (and cheapest / for free) alternative is via Google Maps Geocoding API.

Request / Response

The structure of the request is quite easy – via Http GET:

http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true

The Address has to be Url-Encoded.

After this call a JSON will answer. There is also the option with XML. In my case I’ve decided for JSON and parse the whole thing on a JSON.NET library with a “LINQ-ro-JSON” (or something like that).

If something is found (and Google founds something every time even how weird the places are formatted) than you are going to receive an answer like this:

image1376

 

About the code:

public class GeoCoordinates
    	{
        	public string Lat { get; set; }
        	public string Lng { get; set; }
        	public string Name { get; set; }
		}

		public GeoCoordinates GetCoordinates(string location)
        {
            string url = "http://maps.googleapis.com/maps/api/geocode/json?address=" + HttpUtility.UrlEncode(location) +
                         "&sensor=true";
            WebClient client = new WebClient();
            JObject result = JObject.Parse(client.DownloadString(url));
            JToken status;
            result.TryGetValue("status", out status);

            if(status.ToString() == "ZERO_RESULTS")
                return new GeoCoordinates();

            var geoCood = result.SelectToken("results[0].geometry.location");

            string lat = geoCood.SelectToken("lat").ToString();
            string lng = geoCood.SelectToken("lng").ToString();
            return new GeoCoordinates() { Name = location,  Lat = lat, Lng = lng };
        }

All in all it’s quite easy.

What kinds of restrictions are possible?

It’s only possible to geocode 2500 addresses per day. Beside according to the Terms of Service it’s only allowed if you set a Google Map into the UI or don’t abuse with the API (for example take a lot of files without any sense). More information’s on Google.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

About the author

Written by Code Inside Team

Learn more about our team.

Comment on this post

Recent Posts

  • image1522-540x194.png
    The Retina/HiRes Displays are comming! HD-Web ahoi!

      The best part of the IPad3? The Display. After years of waiting for High Resolution Display there are now some movements in the market. Of course this has an important influence on web-development because nobody wants to see washed-out Pictures or even worse: a totally broken design. How am I able to identify a ...

  • image1528-570x194_thumb.png
    Introduction to Redis on Windows & Redis usage with .NET

      Redis belongs to the NoSQL data banks and you will find it in the group of Key-Value Stores. Redis is often named “Blazing Fast” and according to the Stackoverflow Thread it is used to be two time (while writing) and three times (while reading) quicker than MongoDB. Even if the comparison is a little ...

  • Automated Security Analyser for ASP.NET websites

    Evil Hackers are lurking everywhere and many Web-applications are delicately and share “too much” with the attacker. A quick (first!) overview offers the Tool “ASafaWeb”. All the website does is making a few requests and writing an Analyses including problem solving’s. There are no permanent disadvantages (bad requests/ DoS attacks and so on). Example: KnowYourStack.com ...

  • image1489-570x194.png
    „Sign in with Twitter“ for your own ASP.NET WebApp

      “Sign in with Twitter” is a popular practice to authenticate the users on your website. One advantage compared to an own registration is the lower inhibition for the user. But on the other hand Twitter doesn’t fess up with all the information’s and you will get into a kind of addiction. At the end ...

  • image1485-570x194_thumb.png
    CodePlex is going to be updated

      CodePlex the Microsoft Open Source Project Hosting Plattform hasn’t changed that much in the last few years and for a few times I thought Microsoft stopped the whole developing process. But now I found out that there is still life in the project. Maybe it is because of the success of GitHub or because ...

Support us