3テラバイト

プログラム関連備忘録サイト。主にゲーム。

Unity

簡易逆ジオコーディングサービスをUnityで使う

投稿日:

農研機構が提供しているWebサービス、簡易逆ジオコーディングサービスをUnityから使用してみます。
https://aginfo.cgk.affrc.go.jp/rgeocode/index.html.ja

確認環境

Unity 2019.4.28f1

コード

以下のスクリプトをアクティブなオブジェクトにアタッチして再生すると、受け取ったレスポンスの内容、都道府県コード、都道府県名をコンソールに出力します。

レスポンスはデフォルトのXML形式で受け取り、XDocumentでパースしてデータにアクセスしています。

変数keyValuePairsでリクエストパラメータを設定しているため、lat, lonを変更で別の場所の情報が取得できます。

using Mapbox.Unity.Location;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using UnityEngine;
using UnityEngine.Networking;

public class ReverseGeocoding : MonoBehaviour
{
	const string ApiUrl = "https://aginfo.cgk.affrc.go.jp/ws/rgeocode.php";

	void Start()
	{
			string requestUrl = ApiUrl;

			Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
			keyValuePairs["lat"] = "34.6";
			keyValuePairs["lon"] = "133";
			keyValuePairs["ax"] = "10";
			keyValuePairs["ar"] = "1000";
			keyValuePairs["opt"] = "jpr";

			if (keyValuePairs.Count > 0)
			{
				string paramJoinedStr = string.Join("&", keyValuePairs.Select(pair => string.Format("{0}={1}", pair.Key, pair.Value)));
				requestUrl = string.Format("{0}?{1}", requestUrl, paramJoinedStr);
			}

			StartCoroutine(GetRequest(requestUrl));
		}
	}

	IEnumerator GetRequest(string uri)
	{
		using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
		{
			yield return webRequest.SendWebRequest();

			if (webRequest.isNetworkError)
			{
				Debug.Log("Error: " + webRequest.error);
			}
			else
			{
				Debug.Log("Received: " + webRequest.downloadHandler.text);

				XDocument xml = XDocument.Parse(webRequest.downloadHandler.text);
				XNamespace nsSys = "http://aginfo.cgk.affrc.go.jp/ts";

				XElement rgeocode = xml.Element(nsSys + "rgeocode");
				if (rgeocode != null)
				{
					XElement result = rgeocode.Element(nsSys + "result");
					if (result != null)
					{
						XElement prefecture = result.Element(nsSys + "prefecture");
						if (prefecture != null)
						{
							XElement pcode = prefecture.Element(nsSys + "pcode");
							if (pcode != null)
							{
								Debug.Log("pcode.Value : " + pcode.Value);
							}
							XElement pname = prefecture.Element(nsSys + "pname");
							if (pname != null)
							{
								Debug.Log("pname.Value : " + pname.Value);
							}
						}
					}
				}
			}
		}
	}
}

リンク

Webサービス ご使用条件 / Finds.jp
https://aginfo.cgk.affrc.go.jp/info/ws_tou.html.ja

Networking.UnityWebRequest-Get – Unity スクリプトリファレンス
https://docs.unity3d.com/ja/current/ScriptReference/Networking.UnityWebRequest.Get.html

XDocument.Parse メソッド (System.Xml.Linq) | Microsoft Docs
https://docs.microsoft.com/ja-jp/dotnet/api/system.xml.linq.xdocument.parse?view=net-5.0

-Unity


comment

メールアドレスが公開されることはありません。

関連記事

no image

Unityでスクリプトからゲームの再生や停止を行う

こんにちは、今日はUnityでスクリプトからゲームの再生や停止を行う方法についてまとめていきます。 エディタの再生、一時停止、停止ボタンの処理をスクリプトから行うことができます。これらの処理はエディタ …

no image

Unityのオブジェクトが使用しているメモリ量を取得する

こんにちは、今日はUnityのオブジェクトが使用しているメモリ量を取得する方法についてまとめていきます。 確認環境 Unity 2018.4.15f1 Unityオブジェクトの使用メモリ量取得 以下の …

no image

Unity – キー入力を取得する方法まとめ

こんにちは、今日はUnityでキー入力を取得する方法についてまとめていきます。 確認環境 Unity 2018.4.15f1 キー入力の取得 https://docs.unity3d.com/ja/c …

no image

UnityのHierarchyの順序をスクリプトから変更する

こんにちは、今日はUnityのHierarchyの順序をスクリプトから変更する方法についてまとめていきます。 確認環境 Unity 2018.4.15f1 Hierarchyの順序をスクリプトから変更 …

no image

UnityでuGUIのButtonサイズを子のTextのサイズに自動で合わせる

こんにちは、今日はUnityのuGUIでButtonのサイズを、子オブジェクトのTextのサイズに合わせて変更する方法についてまとめていきます。 確認環境 Version 2018.4.15f1 実装 …