利用 Google Maps 查詢地址經緯度 - Geocoding via HTTP 簡易範例
使用 WebClient 類別:
1: // Taipei 101
2: string address = "台北市信義路五段七號101樓";
3:
4: // 查詢經緯度
5: string output = "csv";
6: string key = "ABQIAAAAXDq__hWKi9eMCwnn7LrMCxT2yXp_ZAY8_ufC3CFXhHIE1NvwkxSnSVp_Xlsd4Ph5iyMua7PE5E0x_A";
7: string url = string.Format("http://maps.google.com/maps/geo?q={0}&output={1}&key={2}", address, output, key);
8:
9: WebClient wc = new WebClient();
10:
11: // 讀取結果
12: Stream s = wc.OpenRead(url);
13: StreamReader sr = new StreamReader(s, Encoding.UTF8);
14: string result = sr.ReadToEnd();
15:
16: // 解析 200,8,25.033408,121.564099 (HTTP status code, accuracy, latitude, longitude)
17: string[] tmpArray = result.Split(',');
18: string latitude = tmpArray[2];
19: string longitude = tmpArray[3];
20:
21: MessageBox.Show(string.Format("緯度: {0}, 經度: {1}", latitude, longitude), address,
22: MessageBoxButtons.OK, MessageBoxIcon.Information);
1: // Taipei 101
2: string address = "台北市信義路五段七號101樓";
3:
4: // 查詢經緯度
5: string output = "csv";
6: string key = "ABQIAAAAXDq__hWKi9eMCwnn7LrMCxT2yXp_ZAY8_ufC3CFXhHIE1NvwkxSnSVp_Xlsd4Ph5iyMua7PE5E0x_A";
7: string url = string.Format("http://maps.google.com/maps/geo?q={0}&output={1}&key={2}", address, output, key);
8:
9: // 送出要求
10: HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
11:
12: // 取得回應
13: HttpWebResponse response = (HttpWebResponse) request.GetResponse();
14:
15: // 讀取結果
16: StreamReader sr = new StreamReader(response.GetResponseStream());
17:
18: // 解析 200,8,25.033408,121.564099 (HTTP status code, accuracy, latitude, longitude)
19: string[] tmpArray = sr.ReadToEnd().Split(',');
20: string latitude = tmpArray[2];
21: string longitude = tmpArray[3];
22:
23: MessageBox.Show(string.Format("緯度: {0}, 經度: {1}", latitude, longitude), address,
24: MessageBoxButtons.OK, MessageBoxIcon.Information);
延伸閱讀: