スポンサーリンク
スポンサーリンク

bing mapを使い、住所から緯度経度を取得する、サンプルC#ソース

bing map

string str住所 = "東京都新宿区西新宿2-8-1";
double f緯度 = 0;
double f経度 = 0;
C01MAP.Get緯度経度(str住所, ref f緯度, ref f経度);

public static void Get緯度経度(string strAdress, ref double dblLatitude, ref double dblLongitude)
{
 string BingMapsKey = "INSERT_YOUR_BING_MAPS_KEY";
 string strURL = string.Format("http://dev.virtualearth.net/REST/v1/Locations?query={0}&key={1}&c=ja-jp&o=xml", strAdress, BingMapsKey);
 XmlDocument geocodeResponse = GetXmlResponse(strURL);

 XmlNamespaceManager nsmgr = new XmlNamespaceManager(geocodeResponse.NameTable);
 nsmgr.AddNamespace("rest", "http://schemas.microsoft.com/search/local/ws/rest/v1");
 XmlNodeList locationElements = geocodeResponse.SelectNodes("//rest:Location", nsmgr);
 string latitude = null;
 string longitude = null;
 if (locationElements.Count == 0)
 {
latitude = "0";
longitude = "0";
 }
 else
 {
XmlNodeList displayGeocodePoints = locationElements[0].SelectNodes(".//rest:GeocodePoint/rest:UsageType[.='None']/parent::node()", nsmgr);
latitude = displayGeocodePoints[0].SelectSingleNode(".//rest:Latitude", nsmgr).InnerText;
longitude = displayGeocodePoints[0].SelectSingleNode(".//rest:Longitude", nsmgr).InnerText;
 }

 dblLatitude = Convert.ToDouble(latitude);
 dblLongitude = Convert.ToDouble(longitude);
}

private static XmlDocument GetXmlResponse(string requestUrl)
{
 Trace.WriteLine("Request URL (XML): " + requestUrl);
 HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;

 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
 {
if (response.StatusCode != HttpStatusCode.OK)
 throw new Exception(String.Format("Server error (HTTP {0}: {1}).", response.StatusCode, response.StatusDescription));

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(response.GetResponseStream());

return xmlDoc;
 }
}

コメント

タイトルとURLをコピーしました