我想在我的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
},
但是我怎麼做到這一點?
任何幫助表示讚賞,並提前:-)
傑夫嗨,由於某種原因,我不能做這項工作: - /我不知道做什麼用的geometryObject以及如何lat和從這個。另一個例子,我做了工作。 – Mansa
哎呀!我完全不好 - 我的'Geometry'在我的腦海中陷入了終結。您需要首先反序列化爲一個Result對象,然後從'resultObject.Geometry'等等訪問屬性,等等。我已經爲你更新了我的答案:) –
哦,是的,但現在我得到這條線上的錯誤:var location = geometry.location; – Mansa