摘要
Google Maps API Web Services,是一個爲您的地圖應用程序提供地理數據的 Google 服務的 HTTP 接口集合。具體包括:Google Geocoding API、Google Directions API、Google Elevation API、Google Places API。本文將探討如何經過Google Geocoding API服務來獲取地址信息。javascript
目錄
1. 什麼是網絡服務?
Google Maps API 提供這些網絡服務做爲從外部服務中請求 Google Maps API 數據以及在您的地圖應用程序中使用它們的接口。這些網絡服務使用特定網址的 HTTP 請求並將網址參數做爲參數提供給服務。通常來說,這些服務會在 HTTP 請求中以 JSON 或 XML 的形式傳回數據,供您的應用程序進行解析和/或處理。java
一個典型的網絡服務請求一般採用如下形式:node
http://maps.google.com/maps/api/service/output?parameters
其中 service
表示所請求的特定服務,output
表示響應格式(一般爲 json
或 xml
)。web
2.地址解析與反地址解析
地址解析是將地址(如「1600 Amphitheatre Parkway, Mountain View, CA」)轉換爲地理座標(如緯度 37.423021 和經度 -122.083739)的過程,您能夠根據轉換獲得的座標放置標記或定位地圖。Google Geocoding API 可以讓您經過 HTTP 請求直接訪問地址解析器。此外,該服務還可以讓您執行反向操做(將座標轉換爲地址),此過程稱爲「反向地址解析」(地址查詢)。json
3. 地址查詢(反向地址解析)請求
Google Geocoding API 請求必須採用如下形式:api
http://maps.google.com/maps/api/geocode/output?parameters
其中,output
能夠是如下值之一:瀏覽器
json
(建議)表示以 JavaScript 對象表示法 (JSON) 的形式輸出xml
表示以 XML 的形式輸出
有些參數是必需的,有些是可選的。根據網址的標準,全部參數均使用字符 & (&
) 分隔。下面枚舉了這些參數及其可能的值。網絡
Google Geocoding API 使用如下網址參數定義地址查詢請求:ide
latlng
(必需)- 您但願獲取的、距離最近的、可人工讀取地址的緯度/經度文本值。bounds
(可選)- 要在其中更顯著地偏移地址解析結果的可視區域的邊框。region
(可選)- 區域代碼,指定爲 ccTLD(「頂級域」)雙字符值。language
(可選)- 傳回結果時所使用的語言。請注意,咱們會常常更新支持的語言,所以該列表可能並不詳盡。若是未提供language
,地址解析器將嘗試儘量使用發送請求的區域的本地語言。sensor
(必需)- 指示地址解析請求是否來自裝有位置傳感器的設備。該值必須爲true
或false
。
注意:bounds
和 region
參數只會影響地址解析器返回的結果,但不能對其進行徹底限制。post
實例一:建立查詢座標(39.910093,116.403945)的地址信息的請求,要求以xml格式輸出響應,語言爲簡體中文(zh-CN)。
http://maps.google.com/maps/api/geocode/xml?latlng=39.910093,116.403945&language=zh-CN&sensor=false
注意:經緯度書寫的順序爲(緯度,經度)。
實例二:利用C#在客戶端程序中建立上述請求。
1 WebClient client = new WebClient();
2 string url = "http://maps.google.com/maps/api/geocode/xml?latlng=39.910093,116.403945&language=zh-CN&sensor=false";
3 client.Encoding = Encoding.UTF8;
4 string responseTest = client.DownloadString(url);
4. 地址查詢(反向地址解析)響應
地址解析響應將以網址請求路徑中的 output
標記所指示的格式傳回。XML 響應包含一個 <GeocodeResponse>
和兩個頂級元素:
<status>
包含請求中狀態代碼。(很是重要)- 零或多個
<result>
元素,每一個元素都包含單獨的一組地址解析地址信息和幾何圖形信息。
地址解析響應對象中的 "status"
字段包含請求的狀態,而且可能包含調試信息,以幫助您追溯地址解析未正常工做的緣由。
"status"
字段可能包含如下值:
"OK"
表示未發生錯誤;地址成功進行了解析而且至少傳回了一個地址解析結果。(判斷請求是否成功響應)"ZERO_RESULTS"
表示地址解析成功,但未返回結果。若是地址解析過程當中傳遞的偏遠位置address
或latlng
並不存在,則會出現這種狀況。"OVER_QUERY_LIMIT"
表示您超出了配額。"REQUEST_DENIED"
表示您的請求被拒絕,一般是因爲缺乏sensor
參數。"INVALID_REQUEST"
一般表示缺乏查詢參數(address
或latlng
)。
實例一:在IE瀏覽中輸入上述實例一中的請求,查看響應結果。
瀏覽器中顯示以下信息(該截圖只是響應結果的部分信息):
實例二:經過控制檯輸出上述實例二的響應。
C#代碼:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Net;
6
7 namespace GeoCodeTest
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 WebClient client = new WebClient();
14 string url = "http://maps.google.com/maps/api/geocode/xml?latlng=39.910093,116.403945&language=zh-CN&sensor=false";
15 client.Encoding = Encoding.UTF8;
16 string responseTest = client.DownloadString(url);
17
18 Console.Write("{0}",responseTest);
19
20 Console.Read();
21
22 }
23 }
24 }
輸出結果(該截圖只是響應結果的部分信息):
5. 處理響應結果
經過上述內容,咱們已經可以獲得xml響應信息。可是,響應結果包含不少信息,所以咱們須要解析出須要的地址信息。具體實現過程爲:
第一步:判斷status的狀態信息。
第二步:獲取formatted_address
地址信息。
注意:formatted_address
是一個字符串,包含此位置的人類可讀地址。一般該地址至關於「郵政地址」,有時會因不一樣國家/地區而存在差別。
實現代碼以下:
![](http://static.javashuo.com/static/loading.gif)
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Net;
6 using System.Xml;
7
8 namespace GeoCodeTest
9 {
10 class Program
11 {
12 static void Main(string[] args)
13 {
14 WebClient client = new WebClient();//webclient客戶端對象
15 string url = "http://maps.google.com/maps/api/geocode/xml?latlng=39.910093,116.403945&language=zh-CN&sensor=false";//請求地址
16 client.Encoding = Encoding.UTF8;//編碼格式
17 string responseTest = client.DownloadString(url);//下載xml響應數據
18
19 XmlDocument doc = new XmlDocument();//建立XML文檔對象
20
21 if(!string.IsNullOrEmpty(responseTest))
22 {
23 doc.LoadXml(responseTest);//加載xml字符串
24
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 //獲取地址信息
33 xpath = @"GeocodeResponse/result/formatted_address";
34 node = doc.SelectSingleNode(xpath);
35 string address = node.InnerText.ToString();
36
37 Console.WriteLine("地址:{0}",address);//輸出地址信息
38 }
39
40 }
41
42
43 Console.Read();
44
45 }
46 }
47 }
輸出結果: