Google Map座標からアドレス情報を取得

6238 ワード

 1 using System;

 2 using System.Collections.Generic;

 3 using System.Linq;

 4 using System.Text;

 5 using System.Xml;

 6 using System.Net;

 7 

 8 namespace Utility

 9 {

10     public class GoogleMapHelper

11     {

12         public string GetAddress(string lat, string lng)

13         {

14             WebClient client = new WebClient();

15             string url = string.Format("http://maps.google.com/maps/api/geocode/xml?latlng={0},{1}&language=zh-CN&sensor=false", lat, lng);

16             client.Encoding = Encoding.UTF8;

17             try

18             {

19                 string responseTest = client.DownloadString(url);

20                 XmlDocument doc = new XmlDocument();

21 

22                 if (!string.IsNullOrEmpty(responseTest))

23                 {

24                     doc.LoadXml(responseTest);

25 

26                     string xpath = @"GeocodeResponse/status";

27                     XmlNode node = doc.SelectSingleNode(xpath);

28                     string status = node.InnerText.ToString();

29 

30                     if (status == "OK")

31                     {

32                         xpath = @"GeocodeResponse/result/formatted_address";

33                         node = doc.SelectSingleNode(xpath);

34                         string address = node.InnerText.ToString();

35                         return address;

36                     }

37                 }

38             }

39             catch

40             {

41             }

42             return "";

43         }

44     }

45 }