Google Geocoding — Obtain Latitude and Longitude for a given address,city and state. A C# example.

All details about Google Geocoding can be found here. http://code.google.com/apis/maps/documentation/geocoding/

When I load the example link, present in that Google link above, (http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false) — I get the following JSON.

{
   “results” : [
      {
         “address_components” : [
            {
               “long_name” : “1600”,
               “short_name” : “1600”,
               “types” : [ “street_number” ]
            },
            {
               “long_name” : “Amphitheatre Pkwy”,
               “short_name” : “Amphitheatre Pkwy”,
               “types” : [ “route” ]
            },
            {
               “long_name” : “Mountain View”,
               “short_name” : “Mountain View”,
               “types” : [ “locality”, “political” ]
            },
            {
               “long_name” : “Santa Clara”,
               “short_name” : “Santa Clara”,
               “types” : [ “administrative_area_level_2”, “political” ]
            },
            {
               “long_name” : “California”,
               “short_name” : “CA”,
               “types” : [ “administrative_area_level_1”, “political” ]
            },
            {
               “long_name” : “United States”,
               “short_name” : “US”,
               “types” : [ “country”, “political” ]
            },
            {
               “long_name” : “94043”,
               “short_name” : “94043”,
               “types” : [ “postal_code” ]
            }
         ],
         “formatted_address” : “1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA”,
         “geometry” : {
            “location” : {
               “lat” : 37.42114440,
               “lng” : -122.08530320
            },
            “location_type” : “ROOFTOP”,
            “viewport” : {
               “northeast” : {
                  “lat” : 37.42249338029150,
                  “lng” : -122.0839542197085
               },
               “southwest” : {
                  “lat” : 37.41979541970850,
                  “lng” : -122.0866521802915
               }
            }
         },
         “types” : [ “street_address” ]
      }
   ],
   “status” : “OK”
}

As you can see, under geometry->location, I have latitude and longitude information. To read the above JSON string in C#, I need to deserialize it in a object. Hence I need to develop my .Net class, whose structure/fieldnames should be in accordance with the above JSON. Here’s how that class under my AppCode folder looks like.

public class MyGeoCodeResponse
{
    public string status { get; set; }
    public results[] results { get; set; }
}

public class results
{
    public string formatted_address { get; set; }
    public _geometry geometry { get; set; }
    public string[] types { get; set; }
    public _address_component[] address_components { get; set; }
}

public class _geometry
{
    public string location_type { get; set; }
    public _location location { get; set; }
    public _bounds bounds { get; set; }
}

public class _location
{
    public string lat { get; set; }
    public string lng { get; set; }
}

public class _bounds
{
    public _northeast northeast { get; set; }
    public _southwest southwest { get; set; }
}

public class _northeast
{
    public string lat { get; set; }
    public string lng { get; set; }
}

public class _southwest
{
    public string lat { get; set; }
    public string lng { get; set; }
}

public class _address_component
{
    public string long_name { get; set; }
    public string short_name { get; set; }
    public string[] types { get; set; }
}

Then next, in the codebehind, I did 2 things —
1) Programmatically invoke the geocode api with the address,city,state as querystring parameters.
2) Deserialize the obtained JSON into my local .Net object. That’s basically it.
Here’s the codebehind

public partial class JsonDeserialize : System.Web.UI.Page
{
public static string url = "http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false";
protected void Page_Load(object sender, EventArgs e)
{
    GetLatitudeLongitude();
}

private void GetLatitudeLongitude()
{
    string result = string.Empty;
    //Get geocode response
    WebClient Client = new WebClient();
    using (Stream strm = Client.OpenRead(url))
    {
        StreamReader sr = new StreamReader(strm);
        result = sr.ReadToEnd();
    }
    //Deserialize into .Net object
    JavaScriptSerializer ser = new JavaScriptSerializer();
    MyGeoCodeResponse _MyGeoCodeResponse = ser.Deserialize<MyGeoCodeResponse>(result);

    string _latitude = _MyGeoCodeResponse.results[0].geometry.location.lat;
    string _longitude = _MyGeoCodeResponse.results[0].geometry.location.lng;
}
}

Here’s how the deserialized object , in Debug->QuickWatch window appears.

img1

Thanks for reading.

Advertisements
This entry was posted in External Open Source APIs. Bookmark the permalink.

One Response to Google Geocoding — Obtain Latitude and Longitude for a given address,city and state. A C# example.

  1. Kurt Amstutz says:

    Nice job. Got my code up and running using your example. Many thanks for your work on this.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s