运用了Geocoding API,它包括地址解析和逆地址解析功能。php
地址解析是指,由详细到街道的结构化地址获得百度经纬度信息,且支持名胜古迹、标志性建筑名称直接解析返回百度经纬度。例如:“北京市海淀区中关村南大街27号”地址解析的结果是“lng:116.31985,lat:39.959836”,“百度大厦”地址解析的结果是“lng:116.30815,lat:40.056885”html
逆地址解析是指,由百度经纬度信息获得结构化地址信息。例如:“lat:31.325152,lng:120.558957”逆地址解析的结果是“江苏省苏州市虎丘区塔园路318号”。注意:web
1.由于Geocoding和反Geocoding使用的门址数据以及算法都不是同样的,因此会出现不能一一对应的现象。算法
2.解析过程当中可能会出现一对坐标值对应多个地址门牌信息,本接口将返回距离坐标点最近的一个地址门牌信息。json
使用方法:api
第一步,申请key,去百度开发者平台http://lbsyun.baidu.com/apiconsole/key申请AK(用户密钥),申请key须要注册百度帐号;网络
申请时请求校验方式选择IP白名单检验,若设置为0.0.0.0/0 则表明不作任何限制。ide
第二步,拼写发送http请求的url,注意需使用第一步申请的key;ui
第三步,接收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 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服务说明见其主页: