Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parse google maps geocode json response to object using Json.Net

I have a DB full of addresses I need to get lat and long for, so I want to loop through them and use Google Geocode to update my database. I am stuck as to how to parse the JSOn result to get what I need:

var address = "http://maps.google.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false";
var result = new System.Net.WebClient().DownloadString(address);
GoogleGeoCodeResponse test = JsonConvert.DeserializeObject<GoogleGeoCodeResponse>(result);

I thought I could simply build a quick class and use JSON.Net to deserialize the result, and it is kind of working but I think I am blowing it on my class structure:

public  class GoogleGeoCodeResponse {

    public string status { get; set; }
    public geometry geometry { get; set; }

}

public class geometry {
    public string location_type { get; set; }
    public location location { get; set; }
}

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

Here is a sample of what get's returned from Google:

{
  "status": "OK",
  "results": [ {
    "types": [ "street_address" ],
    "formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
    "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": "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" ]
    } ],
    "geometry": {
      "location": {
        "lat": 37.4219720,
        "lng": -122.0841430
      },
      "location_type": "ROOFTOP",
      "viewport": {
        "southwest": {
          "lat": 37.4188244,
          "lng": -122.0872906
        },
        "northeast": {
          "lat": 37.4251196,
          "lng": -122.0809954
        }
      }
    }
  } ]
}

I am missing simple here I know it, anyone?

like image 225
Slee Avatar asked Jun 08 '10 20:06

Slee


6 Answers

I tried this, made a simple test and it worked (added results and other):

public class GoogleGeoCodeResponse
{

    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 class location
{
    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; }
}
like image 107
Antonio Avatar answered Nov 13 '22 18:11

Antonio


You can use a dynamic object rather than defining the object.

 public static dynamic GEOCodeAddress(String Address)
    {
        var address = String.Format("http://maps.google.com/maps/api/geocode/json?address={0}&sensor=false", Address.Replace(" ", "+"));
        var result = new System.Net.WebClient().DownloadString(address);
        JavaScriptSerializer jss = new JavaScriptSerializer();
        return jss.Deserialize<dynamic>(result);
    }
like image 32
matt.j.crawford Avatar answered Nov 13 '22 17:11

matt.j.crawford


i have done something similar refer to Google Geo Kit

like image 4
jebberwocky Avatar answered Nov 13 '22 17:11

jebberwocky


C# Object Code I added a few extra classes, not sure if they are new to the API but I thought this might be helpful to someone.

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

    }

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

    public class address_component
    {
        String long_name { get; set; }
        String short_name { get; set; }
        String types { get; set; }

    }

    public class geometry
    {
        public bounds bounds { get; set; }
        public location location { get; set; }
        public string location_type { get; set; }
        public viewport viewport { get; set; }
    }

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

    public class viewport
    {
        public northeast northeast { get; set; }
        public southwest southwest { get; set; }
    }

    public class bounds
    {
        public northeast northeast { 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; }
    }
like image 3
JEuvin Avatar answered Nov 13 '22 19:11

JEuvin


Thanks to JEuvin above, I was able to easily switch from XML to Json with a few mods to the code above (specifically changing lat and lng to decimal or double) and also had to change address_components.types to string[] to get it to work for me. I then refactored a bit so that the same classes can be deserialized from XML or Json interchangeably.

Maybe this will help someone too...

using System;
using System.Xml.Serialization;

[Serializable]
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false)]
public class GeocodeResponse
{
    public GeocodeResponse()
    {
    }

    [XmlElement("result")]

    public results[] results { get; set; }

    public string status { get; set; }
}

[XmlType(AnonymousType = true)]
public class results
{
    public results()
    {
    }

    [XmlElement("address_component")]

    public address_component[] address_components { get; set; }

    public string formatted_address { get; set; }

    public geometry geometry { get; set; }

    [XmlElement("type")]
    public string[] types { get; set; }

    public string[] postcode_localities { get; set; }

    public bool partial_match { get; set; }

    public string place_id { get; set; }
}

[XmlType(AnonymousType = true)]
public class address_component
{
    public address_component()
    {
    }

    public string long_name { get; set; }

    public string short_name { get; set; }

    [XmlElement("type")]
    public string[] types { get; set; }
}

[XmlType(AnonymousType = true)]
public class geometry
{
    public geometry()
    {
    }

    public bounds bounds { get; set; }

    public location location { get; set; }

    public string location_type { get; set; }

    public viewport viewport { get; set; }
}

[XmlType(AnonymousType = true)]
public class location
{
    public location()
    {
    }

    public double lat { get; set; }

    public double lng { get; set; }
}

[XmlType(AnonymousType = true)]
public class viewport
{
    public viewport()
    {
    }

    public northeast northeast { get; set; }

    public southwest southwest { get; set; }
}

[XmlType(AnonymousType = true)]
public class bounds
{
    public bounds()
    {
    }

    public northeast northeast { get; set; }
}

[XmlType(AnonymousType = true)]
public class northeast
{
    public northeast()
    {
    }

    public double lat { get; set; }

    public double lng { get; set; }
}

[XmlType(AnonymousType = true)]
public class southwest
{
    public southwest()
    {
    }

    public double lat { get; set; }

    public double lng { get; set; }
}

(edited with addition of postcode_localities and partial_match properties)

like image 2
planetdonovan Avatar answered Nov 13 '22 17:11

planetdonovan


Just for an update to this with a much easier method of handling what is wanted, all you need to do is the use the following:

var result = new System.Net.WebClient().DownloadString( <<ENTER ADDRESS URL HERE>> );
dynamic geo = JsonConvert.DeserializeObject(result);

then you can access the latitude with: geo.results[0].geometry.location.lat

like image 1
Lee Templeton Avatar answered Nov 13 '22 19:11

Lee Templeton