使用Unity WWW课程从谷歌获取lat和lng

时间:2016-06-21 20:59:31

标签: c# json google-maps unity3d

我正在尝试使用我的Unity内部谷歌的地理服务。我是这样做的:

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);
}

这部分工作正常,我得到了我想要的结果......但是知道我只需要从结果中得到纬度和经度,但我不知道该怎么做?

以下是我从谷歌获得的结果:

{
  "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"
}

我想我需要这样做:

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

但是我该怎么做?

任何帮助都表示赞赏,并提前感谢: - )

2 个答案:

答案 0 :(得分:2)

  

我想我需要这样做:

"geometry": {
    "location": {
      "lat": 37.422364,
      "lng": -122.084364
    },
你是对的。您需要Json中的geometry对象。我之前使用Google的API来取回对象。

您需要在C#中创建一些类,这些类映射到Json对象的相同/相关属性。然后你可以使用JsonConvert将你的Json字符串转换回一些C#对象 - http://www.newtonsoft.com/json/help/html/t_newtonsoft_json_jsonconvert.htm

一个很好用的工具是http://json2csharp.com/,它允许你粘贴你的Json代码,并从另一端获得一些可识别的C#类。

显然,你可以删除任何你绝对不需要的属性。

你的最终结果应该是这样的(复制/粘贴到/从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; }
}

然后使用JsonConvert创建一些对象,你可以这样做:

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

然后,您可以通过迭代它们(或访问Geometry)等来访问Result中的.First()个对象。

像这样:

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

为了整理,您还可以重命名一些属性以使它们更“友好”,然后使用属性装饰它们,以便JsonConvert仍然知道要映射哪个Json属性。

像这样:

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

有任何问题,请告诉我。

希望这有帮助! :)

答案 1 :(得分:1)

您需要创建一个数据结构来存储Json数据。使用Unity的JsonUtility.FromJson,您可以提取json数据并将其存储到该数据结构中。

请记住,由于结果是array / list,因此您必须遍历它以获取所有值。您必须从每个类中删除{ get; set; },并且还必须在每个类的顶部包含[System.Serializable]。只要您拥有Unity 5.3及更高版本,就不需要外部API。这是经过测试的完整解决方案。

[System.Serializable]
public class AddressComponent
{
    public string long_name;
    public string short_name;
    public List<string> types;
}
[System.Serializable]
public class Location
{
    public double lat;
    public double lng;
}
[System.Serializable]
public class Northeast
{
    public double lat;
    public double lng;
}
[System.Serializable]
public class Southwest
{
    public double lat;
    public double lng;
}
[System.Serializable]
public class Viewport
{
    public Northeast northeast;
    public Southwest southwest;
}
[System.Serializable]
public class Geometry
{
    public Location location;
    public string location_type;
    public Viewport viewport;
}
[System.Serializable]
public class Result
{
    public List<AddressComponent> address_components;
    public string formatted_address;
    public Geometry geometry;
    public string place_id;
    public List<string> types;
}
[System.Serializable]
public class GoogleJson
{
    public List<Result> results;
    public string status;
}

使用它:

void Start()
{
    //Replace value with what you got from WWW
    string value = "{\r\n  \"results\": [\r\n    {\r\n      \"address_components\": [\r\n        {\r\n          \"long_name\": \"1600\",\r\n          \"short_name\": \"1600\",\r\n          \"types\": [\r\n            \"street_number\"\r\n          ]\r\n        },\r\n        {\r\n          \"long_name\": \"Amphitheatre Parkway\",\r\n          \"short_name\": \"Amphitheatre Pkwy\",\r\n          \"types\": [\r\n            \"route\"\r\n          ]\r\n        },\r\n        {\r\n          \"long_name\": \"Mountain View\",\r\n          \"short_name\": \"Mountain View\",\r\n          \"types\": [\r\n            \"locality\",\r\n            \"political\"\r\n          ]\r\n        },\r\n        {\r\n          \"long_name\": \"Santa Clara County\",\r\n          \"short_name\": \"Santa Clara County\",\r\n          \"types\": [\r\n            \"administrative_area_level_2\",\r\n            \"political\"\r\n          ]\r\n        },\r\n        {\r\n          \"long_name\": \"California\",\r\n          \"short_name\": \"CA\",\r\n          \"types\": [\r\n            \"administrative_area_level_1\",\r\n            \"political\"\r\n          ]\r\n        },\r\n        {\r\n          \"long_name\": \"United States\",\r\n          \"short_name\": \"US\",\r\n          \"types\": [\r\n            \"country\",\r\n            \"political\"\r\n          ]\r\n        },\r\n        {\r\n          \"long_name\": \"94043\",\r\n          \"short_name\": \"94043\",\r\n          \"types\": [\r\n            \"postal_code\"\r\n          ]\r\n        }\r\n      ],\r\n      \"formatted_address\": \"1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA\",\r\n      \"geometry\": {\r\n        \"location\": {\r\n          \"lat\": 37.422364,\r\n          \"lng\": -122.084364\r\n        },\r\n        \"location_type\": \"ROOFTOP\",\r\n        \"viewport\": {\r\n          \"northeast\": {\r\n            \"lat\": 37.4237129802915,\r\n            \"lng\": -122.0830150197085\r\n          },\r\n          \"southwest\": {\r\n            \"lat\": 37.421015019708513,\r\n            \"lng\": -122.0857129802915\r\n          }\r\n        }\r\n      },\r\n      \"place_id\": \"ChIJ2eUgeAK6j4ARbn5u_wAGqWA\",\r\n      \"types\": [\r\n        \"street_address\"\r\n      ]\r\n    }\r\n  ],\r\n  \"status\": \"OK\"\r\n}";
    GoogleJson gJson = null;
    gJson = JsonUtility.FromJson<GoogleJson>(value);

    for (int i = 0; i < gJson.results.Count; i++)
    {
        Debug.Log("RESULT: " + i);
        Debug.Log("Geometry lat: " + gJson.results[i].geometry.location.lat);
        Debug.Log("Geometry lng: " + gJson.results[i].geometry.location.lng);
    }
}