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

  • 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 ...

  • image1474_thumb.png
    What does Adobe in the flash-free web? Magazine-Style Layouts with CSS Regions!

      Adobe is well known for Photoshop and Flash but of course there is a lot more. According to the “Future Post” from Google Adobe declared one of their big subjects on a Blogpost. I’m talking about the W3C Working Draft to CSS Regions. Adobe cooperates with the WebKit Team and W3C on this. What ...

  • image1471-523x194.png
    HTML 5 Games, Tooling & 3D

      Game Developing is an interesting subject for all kind of software developer. But as a web developer without any Flash-skills there aren’t that much starting points. With HTML5 and the combination between Javascript, CSS3 and fast browsers there are the first “robust” HTML5 games. HTML5 games? Is this real? Neowin created a “Top 10” ...

Support us