百度地圖API根據地名獲取經緯度


運用了Geocoding API,它包括地址解析和逆地址解析功能。

地址解析是指,由詳細到街道的結構化地址得到百度經緯度信息,且支持名勝古跡、標志性建築名稱直接解析返回百度經緯度。例如:“北京市海淀區中關村南大街27號”地址解析的結果是“lng:116.31985,lat:39.959836”,“百度大廈”地址解析的結果是“lng:116.30815,lat:40.056885”

逆地址解析是指,由百度經緯度信息得到結構化地址信息。例如:“lat:31.325152,lng:120.558957”逆地址解析的結果是“江蘇省蘇州市虎丘區塔園路318號”。注意:

1.因為Geocoding和反Geocoding使用的門址數據以及算法都不是一樣的,所以會出現不能一一對應的現象。

2.解析過程中可能會出現一對坐標值對應多個地址門牌信息,本接口將返回距離坐標點最近的一個地址門牌信息。

使用方法:

第一步,申請key,去百度開發者平台http://lbsyun.baidu.com/apiconsole/key申請AK(用戶密鑰),申請key需要注冊百度賬號;

申請時請求校驗方式選擇IP白名單檢驗,若設置為0.0.0.0/0 則代表不做任何限制。

第二步,拼寫發送http請求的url,注意需使用第一步申請的key;

第三步,接收http請求返回的數據(支持json和xml格式)。

百度地圖API服務說明見其主頁:

http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding

地址解析:根據地址獲取坐標

http://api.map.baidu.com/geocoder?address=地址&output=輸出格式類型&key=用戶密鑰&city=城市名

逆地址解析:根據坐標獲取地址

http://api.map.baidu.com/geocoder?location=緯度,經度&output=輸出格式類型&key=用戶密鑰

C#定義一個類Geo

 1 using System.Collections.Generic;
 2 using System.Text;
 3 using System.Net;
 4 using System.IO;
 5 public class Geo
 6     {
 7         /// 
 8         /// latitude
 9         /// 
10         private string _latitude = "";
11 
12         /// 
13         /// longtitude
14         /// 
15         private string _longtitude = "";
16 
17         /// 
18         /// default constructor
19         /// 
20         public Geo()
21         {
22 
23         }
24 
25         /// 
26         ///類Geo提供經緯度
27         /// 
28         public Geo(string latitude, string longtitude)
29         {
30             _latitude = latitude;
31             _longtitude = longtitude;
32         }
33 
34         /// 
35         /// 根據地名獲取經緯度
36         ///  
37         public Geo(string location)
38         {
39             string ak = ".........";//輸入在百度開發者平台免費申請的密鑰
40             string url = string.Format("http://api.map.baidu.com/geocoder/v2/?address={0}&output=json&ak={1}&callback=showLocation", location, ak);
41             try
42             {
43                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
44                 using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
45                 {
46                     using (StreamReader sr = new StreamReader(response.GetResponseStream()))
47                     {
48                         string[] tmpArray = sr.ReadToEnd().Split(new char[2] { ',', ':' });
49                         _latitude = tmpArray[7];//緯度
50                         _longtitude = tmpArray[5];//經度
51                     }
52                 }
53             }
54             catch (System.Net.Sockets.SocketException ex)
55             {
56                 Console.WriteLine("網絡中斷");
57             }
58             catch (Exception ex)
59             {
60                 //throw ex;
61                 Console.WriteLine("異常類型:{0}", ex.GetType());
62                 Console.WriteLine("異常信息:{0}", ex.Message);
63                 Console.WriteLine("異常來源:{0}", ex.Source);
64                 Console.WriteLine("異常堆棧:{0}", ex.StackTrace);
65                 Console.WriteLine("內部異常:{0}", ex.InnerException);
66             }
67         }
68 
69         /// 
70         /// get latitude
71         /// 
72         public string Latitude
73         {
74             get { return _latitude; }
75             set { _latitude = value; }
76         }
77 
78         /// 
79         /// get longtitude
80         /// 
81         public string Longtitude
82         {
83             get { return _longtitude; }
84             set { _longtitude = value; }
85         }
86     }
類Geo定義

調用方法:

Geo position = new Geo(地名);
MessageBox.Show("經度:" + position.Longtitude + ";緯度:" + position.Latitude);//顯示對應經緯度

以上是地址解析方法,逆地址解析方法大同小異。

PS:

百度地圖API每個開發者賬號每天調用“地理編碼”API 服務的總次數(即配額)是有限的,未認證是6000次,認證成功是30萬次。

若想不受限,可以用天地圖API(完全免費,只需輸入地址且無需ak),它的地址解析url如下:

http://api.tianditu.gov.cn/geocoder?ds={"keyWord":"地址"}

(使用時需注意url內的“{}”和雙引號都需要加轉義字符,雙引號前加" \ ",大括號須連續寫兩個,如"{"須寫成"{{",轉義后為一個大括號)

天地圖API服務說明見其主頁:

http://lbs.tianditu.gov.cn/server/guide.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM