Mansa Mansa - 5 months ago 28
JSON Question

Get lat and lng from google using Unity WWW class

I am trying to use the geo service from google inside my Unity. Here is how I do this:

WWW www = new WWW("https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&oe=utf-8&key="+googleKey);
yield return www;

if (!string.IsNullOrEmpty(www.error)){
print(www.error);
} else {
var newobject = JsonConvert.DeserializeObject(www.text);
print ("Object: " + newobject);
}


This part works fine and I get the result I want... But know I need to only get the latitude and longitude out of the result, but am not sure how to do this?

Here is the result I get from google:

{
"results": [
{
"address_components": [
{
"long_name": "1600",
"short_name": "1600",
"types": [
"street_number"
]
},
{
"long_name": "Amphitheatre Parkway",
"short_name": "Amphitheatre Pkwy",
"types": [
"route"
]
},
{
"long_name": "Mountain View",
"short_name": "Mountain View",
"types": [
"locality",
"political"
]
},
{
"long_name": "Santa Clara County",
"short_name": "Santa Clara County",
"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.422364,
"lng": -122.084364
},
"location_type": "ROOFTOP",
"viewport": {
"northeast": {
"lat": 37.4237129802915,
"lng": -122.0830150197085
},
"southwest": {
"lat": 37.421015019708513,
"lng": -122.0857129802915
}
}
},
"place_id": "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
"types": [
"street_address"
]
}
],
"status": "OK"
}


I guess I need to go to this:

"geometry": {
"location": {
"lat": 37.422364,
"lng": -122.084364
},


But how do I do this?

Any help is appreciated and thanks in advance :-)

Answer

I guess I need to go to this:

"geometry": {
    "location": {
      "lat": 37.422364,
      "lng": -122.084364
    },

You're right. You need the geometry object within the Json. I've used Google's API to get objects back before.

You need to create some classes in C# that map to the same/relevant properties from the Json object. Then you can use JsonConvert in order to convert your Json string back to some C# objects -http://www.newtonsoft.com/json/help/html/t_newtonsoft_json_jsonconvert.htm

A great tool to use is http://json2csharp.com/ which allows you to paste your Json code in, and get some recognisable C# classes out the other end.

Obviously, you could remove any properties you definitely don't need.

Your end result should look something like this (copy/pasted to/from json2csharp.com):

public class AddressComponent
{
    public string long_name { get; set; }
    public string short_name { get; set; }
    public List<string> types { get; set; }
}

public class Location
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Northeast
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Southwest
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Viewport
{
    public Northeast northeast { get; set; }
    public Southwest southwest { get; set; }
}

public class Geometry
{
    public Location location { get; set; }
    public string location_type { get; set; }
    public Viewport viewport { get; set; }
}

public class Result
{
    public List<AddressComponent> address_components { get; set; }
    public string formatted_address { get; set; }
    public Geometry geometry { get; set; }
    public string place_id { get; set; }
    public List<string> types { get; set; }
}

public class RootObject
{
    public List<Result> results { get; set; }
    public string status { get; set; }
}

To then use JsonConvert to create some objects, you can do:

RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(www.text);

You can then access your Geometry objects in the Results by iterating over them (or accessing the .First()) etc.

Like this:

foreach (var resultObject in rootObject.results)
{
    var geometry = resultObject.geometry;
    var location = geometry.location;
    var lat = location.lat;
    var lng = location.lng
}

To tidy things up, you can also rename some properties to make them more "friendly", and then decorate them with an attribute, so that JsonConvert still knows which Json property to map.

Like this:

[JsonProperty(PropertyName = "a_json_property")]
public string ACSharpProperty { get; set; }

Any questions, let me know.

Hope this helps! :)