1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
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; } } |
コメント