2016-06-21 7 views
0

私のUnity内でGoogleからgeoサービスを使用しようとしています。ここで私はこれを行う方法です。Unity WWWクラスを使用してGoogleから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 
     }, 

しかし、私はこれをどのように行うのですか。ここ

は、私はGoogleからの取得結果のですか?

すべてのヘルプは

答えて

2

:-)事前に感謝し、感謝されて、私はこれに行く必要が推測:

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

あなたが正しいです。 Json内にgeometryオブジェクトが必要です。以前はオブジェクトを元に戻すためにGoogleのAPIを使用しました。

Jsonオブジェクトと同じ/関連するプロパティにマップするいくつかのクラスをC#で作成する必要があります。使用するhttp://www.newtonsoft.com/json/help/html/t_newtonsoft_json_jsonconvert.htm

素晴らしいツールはあなたがあなたのJSONコードを貼り付け、もう一方の端から、いくつかの認識可能なC#クラスを取得することができますhttp://json2csharp.com/である - そして、あなたはいくつかのC#オブジェクトに戻ってあなたのJSON文字列を変換するためにJsonConvertを使用することができます。

明らかに必要のないプロパティは削除できます。

次のようになりますあなたの最終結果(コピー/ 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); 

ResultGeometryオブジェクトにアクセスするには、それらを反復して(または.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; } 

ご質問、私に知らせてください。

希望すると便利です。 :)

+0

こんにちはジェフ、: - /私はgeometryObjectで何をすべきかわからないと緯度を取得する方法と、これからlng。私がもう一度実践した例もあります。 – Mansa

+0

おっと!完全に私の悪い - 私は最終的なゲームとして私の頭の中で立ち往生 'ジオメトリ'を持っていただろう。最初に '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を解析するために 'dynamic'オブジェクトを使っていました。最終的には本当にうんざりして醜いので、古いC#クラスを選びました。 http://json2csharp.comは命を救うツールです! –

関連する問題