緯度・経度から住所を取得する
以前、緯度・経度から住所を取得できるライブラリとか無いか探したが
見つからなかったのであきらめていた。下記のサイトでそのようなAPIを
提供しているということを知り早速試してみた。
下記のようにコードを書く
ここではレスポンスはXMLで受け取るように設定しています。
XmlDocumentの使い方に慣れていないので変なコードで済みません。
using System; using System.Collections.Generic; using System.Text; using System.Net; // WebClient using System.Xml; // XmlDocument using System.IO; // Stream using System.Windows.Forms; // MessageBox namespace geoPointApiTest { class Program { static void Main(string[] args) { WebClient webClient = new WebClient(); string strFormat = "xml"; string strArea = "35.661123,139.702631"; string strApiKey = {APIキー}; string strUri = string.Format("http://api.cirius.co.jp/v1/geopoint/{0}?area={1}&api_key={2}", strFormat, strArea, strApiKey); Stream stream = webClient.OpenRead(strUri); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(stream); XmlElement rootElement = xmlDocument.DocumentElement; string strCount = rootElement["count"].InnerText; if (int.Parse(strCount) >= 1) { XmlElement areasElement = rootElement["areas"]; XmlElement firstAreaElement = (XmlElement)areasElement.FirstChild; string strPref = firstAreaElement["pref"]["name"].InnerText; string strCity = firstAreaElement["city"]["name"].InnerText; string strTown = firstAreaElement["town"].InnerText; string strDisplay = firstAreaElement["display"].InnerText; string strPlace = strPref + strCity + strTown + strDisplay; MessageBox.Show(strPlace); } } } }
参照
Cilius Lab http://lab.cirius.co.jp/