百度api:根据经纬度获取地理位置信息
調(diào)用百度api,根據(jù)經(jīng)度和緯度獲取地理位置信息,返回Json。
C#代碼:
using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Net.Http;public class LocationService{//百度apiprivate static string url = @"http://api.map.baidu.com/geocoder/v2/?location={0}&output=json&ak=WEc8RlPXzSifaq9RHxE1WW7lRKgbid6Y";/// <summary>/// 根據(jù)經(jīng)緯度獲取地理位置/// </summary>/// <param name="lat">緯度</param>/// <param name="lng">經(jīng)度</param>/// <returns>具體的地埋位置</returns>public static string GetLocation(string lat, string lng){HttpClient client = new HttpClient();string location = string.Format("{0},{1}", lat, lng);string bdUrl = string.Format(url, location);string result = client.GetStringAsync(bdUrl).Result;var locationResult = (JObject)JsonConvert.DeserializeObject(result);if (locationResult == null || locationResult["result"] == null || locationResult["result"]["formatted_address"] == null)return string.Empty;var address = Convert.ToString(locationResult["result"]["formatted_address"]);if (locationResult["result"]["sematic_description"] != null)address += " " + Convert.ToString(locationResult["result"]["sematic_description"]);return address;}}調(diào)用示例1:
LocationService.GetLocation("0","0")
返回Json:
{{ ?"country": "", ?"country_code": -1, ?"province": "", ?"city": "", ?"district": "", ?"adcode": "0", ?"street": "", ?"street_number": "", ?"direction": "", ?"distance": ""}}
調(diào)用示例2:
LocationService.GetLocation("36.2585", "120.27")
返回Json:
?{{ ?"status": 0, ?"result": { ? ?"location": { ? ? ?"lng": 120.26999999999993, ? ? ?"lat": 36.25849989472075 ? ?}, ? ?"formatted_address": "山東省青島市城陽區(qū)和融路", ? ?"business": "上馬", ? ?"addressComponent": { ? ? ?"country": "中國", ? ? ?"country_code": 0, ? ? ?"province": "山東省", ? ? ?"city": "青島市", ? ? ?"district": "城陽區(qū)", ? ? ?"adcode": "370214", ? ? ?"street": "和融路", ? ? ?"street_number": "", ? ? ?"direction": "", ? ? ?"distance": "" ? ?}, ? ?"pois": [], ? ?"roads": [], ? ?"poiRegions": [], ? ?"sematic_description": "青島寶佳自動化設(shè)備有限公司北575米", ? ?"cityCode": 236 ?}}}
?
=================================================================
以下內(nèi)容轉(zhuǎn)自他人博客,返回xml格式的例子
博客地址:http://www.cnblogs.com/_zjl/p/3431525.html
?
代碼:
private string GetAddress(string lng, string lat){try{string url = @"http://api.map.baidu.com/geocoder/v2/?ak=E4805d16520de693a3fe707cdc962045&callback=renderReverse&location=" + lat + "," + lng + @"&output=xml&pois=1";WebRequest request = WebRequest.Create(url);request.Method = "POST";XmlDocument xmlDoc = new XmlDocument();string sendData = xmlDoc.InnerXml;byte[] byteArray = Encoding.Default.GetBytes(sendData);Stream dataStream = request.GetRequestStream();dataStream.Write(byteArray, 0, byteArray.Length);dataStream.Close();WebResponse response = request.GetResponse();dataStream = response.GetResponseStream();StreamReader reader = new StreamReader(dataStream, System.Text.Encoding.GetEncoding("utf-8"));string responseXml = reader.ReadToEnd();XmlDocument xml = new XmlDocument();xml.LoadXml(responseXml);string status = xml.DocumentElement.SelectSingleNode("status").InnerText;if (status == "0"){XmlNodeList nodes = xml.DocumentElement.GetElementsByTagName("formatted_address");if (nodes.Count > 0){return nodes[0].InnerText;}elsereturn "未獲取到位置信息,錯誤碼3";}else{return "未獲取到位置信息,錯誤碼1";}}catch (System.Exception ex){return "未獲取到位置信息,錯誤碼2";}}
url中的參數(shù):
| coordtype | 否 | bd09ll | bd09ll 百度經(jīng)緯度坐標 | 坐標的類型,目前支持的坐標類型包括:bd09ll(百度經(jīng)緯度坐標)、gcj02ll(國測局經(jīng)緯度坐標)、wgs84ll( GPS經(jīng)緯度) |
| location | 是 | 無 | 38.76623,116.43213 lat<緯度>,lng<經(jīng)度> | 根據(jù)經(jīng)緯度坐標獲取地址 |
| pois | 否 | 0 | 0 | 是否顯示指定位置周邊的poi,0為不顯示,1為顯示。當值為1時,顯示周邊100米內(nèi)的poi。 |
?
?
運行方法返回的結(jié)果:
北京市海淀區(qū)中關(guān)村大街27號1101-08室
?
從百度api返回的結(jié)果:
<?xml version="1.0" encoding="utf-8" ?> - <GeocoderSearchResponse><status>0</status> - <result> - <location><lat>39.983424051248</lat> <lng>116.32298703399</lng> </location><formatted_address>北京市海淀區(qū)中關(guān)村大街27號1101-08室</formatted_address> <business>中關(guān)村,人民大學(xué),蘇州街</business> - <addressComponent><streetNumber /> <street>中關(guān)村大街</street> <district>海淀區(qū)</district> <city>北京市</city> <province>北京市</province> </addressComponent><cityCode>131</cityCode> - <pois> - <poi><addr>中關(guān)村西區(qū)南側(cè)(中關(guān)村科技園區(qū)內(nèi))</addr> <distance>0.050000</distance> <name>中關(guān)村大廈</name> <poiType>辦公大廈,商務(wù)大廈</poiType> <tel>(010)82856666</tel> <zip>100000</zip> - <point><x>116.32298658484</x> <y>39.983423843929</y> </point></poi> - <poi><addr>中關(guān)村大街27號</addr> <distance>0.050000</distance> <name>眉州東坡酒樓中關(guān)村店</name> <poiType>中餐館,餐飲</poiType> <tel>(010)82856948</tel> <zip /> - <point><x>116.32298658484</x> <y>39.983423843929</y> </point></poi> - <poi><addr>中關(guān)村大街27號</addr> <distance>0.050000</distance> <name>中國人民財產(chǎn)保險中關(guān)村營業(yè)部</name> <poiType>中國人民財產(chǎn)保險,保險公司,金融</poiType> <tel>(010)82856779</tel> <zip>100000</zip> - <point><x>116.32298658484</x> <y>39.983423843929</y> </point></poi> - <poi><addr>北京市海淀區(qū)</addr> <distance>94.432081</distance> <name>光合作用書房</name> <poiType>圖書音像,購物</poiType> <tel /> <zip /> - <point><x>116.32239334388</x> <y>39.983890240676</y> </point></poi> - <poi><addr>中關(guān)村大街27號</addr> <distance>42.195731</distance> <name>建行中關(guān)村支行</name> <poiType>中國建設(shè)銀行,銀行,金融</poiType> <tel /> <zip>100000</zip> - <point><x>116.32292037972</x> <y>39.983711118168</y> </point></poi> - <poi><addr>北京市海淀區(qū)</addr> <distance>62.342644</distance> <name>海淀醫(yī)院-激光整形美容部</name> <poiType>美容美發(fā),生活服務(wù)</poiType> <tel /> <zip /> - <point><x>116.32317954086</x> <y>39.98301950182</y> </point></poi> - <poi><addr>中關(guān)村大街19號新中關(guān)購物中心1樓</addr> <distance>112.983688</distance> <name>星巴克新中關(guān)店</name> <poiType>星巴克,咖啡廳,休閑餐飲,餐飲</poiType> <tel>(010)82486056</tel> <zip /> - <point><x>116.32218215226</x> <y>39.983899777278</y> </point></poi></pois></result></GeocoderSearchResponse>xml說明:
| status | constant | 返回結(jié)果狀態(tài)值, 成功返回0,其他值請查看附錄。 |
| location | lat | 緯度坐標 |
| lng | 經(jīng)度坐標 | |
| formatted_address | 結(jié)構(gòu)化地址信息 | |
| business | 所在商圈信息,如 "人民大學(xué),中關(guān)村,蘇州街" | |
| addressComponent | city | 城市名 |
| district | 區(qū)縣名 | |
| province | 省名 | |
| street | 街道名 | |
| street_number | 街道門牌號 | |
| pois(周邊poi數(shù)組) | addr | 地址信息 |
| cp | 數(shù)據(jù)來源 | |
| distance | 離坐標點距離 | |
| name | poi名稱 | |
| poiType | poi類型,如’ 辦公大廈,商務(wù)大廈’ | |
| point | poi坐標{x,y} | |
| tel | 電話 | |
| uid | poi唯一標識 | |
| zip | 郵編 | |
附錄:
| 0 | 正常 |
| 1 | 服務(wù)器內(nèi)部錯誤 |
| 2 | 請求參數(shù)非法 |
| 3 | 權(quán)限校驗失敗 |
| 4 | 配額校驗失敗 |
| 5 | ak不存在或者非法 |
| 101 | 服務(wù)禁用 |
| 102 | 不通過白名單或者安全碼不對 |
| 2xx | 無權(quán)限 |
| 3xx | 配額錯誤 |
?
轉(zhuǎn)載于:https://www.cnblogs.com/xianhan/p/7171134.html
總結(jié)
以上是生活随笔為你收集整理的百度api:根据经纬度获取地理位置信息的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 美团app如何扫码骑车(汉典美字的基本解
- 下一篇: 小猿搜题怎么搜题(3699小游戏)