2016-06-21 49 views
0

我想在我的Unity中使用谷歌的地理服務。這是我如何做到這一點:從谷歌使用Unity WWW類獲取lat和lng

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

我想我需要去這得益於:

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

然後,您可以通過遍歷它們(或訪問.First())等訪問Geometry中的對象

與第是:

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

任何問題,讓我知道。

希望這會有所幫助! :)

+0

傑夫嗨,由於某種原因,我不能做這項工作: - /我不知道做什麼用的geometryObject以及如何lat和從這個。另一個例子,我做了工作。 – Mansa

+0

哎呀!我完全不好 - 我的'Geometry'在我的腦海中陷入了終結。您需要首先反序列化爲一個Result對象,然後從'resultObject.Geometry'等等訪問屬性,等等。我已經爲你更新了我的答案:) –

+0

哦,是的,但現在我得到這條線上的錯誤:var location = geometry.location; – Mansa

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

好!那是創建一個'動態'對象嗎? –

+0

@GeoffJames是的。它創建它然後將結果分配給它。如果你不希望它創建該對象的新實例,那麼可以使用'JsonUtility.FromJsonOverwrite',但這意味着在調用該函數之前必須先執行'GoogleJson gJson = new GoogleJson();'。看起來我們都在你的答案中使用了相同的鏈接來完成我們的json工作。 – Programmer

+0

我以爲我認出了它。當我開始爲某個項目做一些Google Geocode素材時,我使用了一個「動態」對象來解析出一些Json。最後它變得非常煩瑣和難看,所以我選擇了一個很好的舊C#類。 http://json2csharp.com是一款拯救生命的工具! –

相關問題