百度api:根據經緯度獲取地理位置信息


調用百度api,根據經度和緯度獲取地理位置信息,返回Json。

C#代碼:

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Net.Http;

    public class LocationService
    {
        //百度api
        private static string url = @"http://api.map.baidu.com/geocoder/v2/?location={0}&output=json&ak=WEc8RlPXzSifaq9RHxE1WW7lRKgbid6Y";

        /// <summary>
        /// 根據經緯度獲取地理位置
        /// </summary>
        /// <param name="lat">緯度</param>
        /// <param name="lng">經度</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;
        }
    }

調用示例1:

LocationService.GetLocation("0","0")

返回Json:

{{  "country": "",  "country_code": -1,  "province": "",  "city": "",  "district": "",  "adcode": "0",  "street": "",  "street_number": "",  "direction": "",  "distance": ""}}

調用示例2:

LocationService.GetLocation("36.2585", "120.27")

返回Json:

 {{  "status": 0,  "result": {    "location": {      "lng": 120.26999999999993,      "lat": 36.25849989472075    },    "formatted_address": "山東省青島市城陽區和融路",    "business": "上馬",    "addressComponent": {      "country": "中國",      "country_code": 0,      "province": "山東省",      "city": "青島市",      "district": "城陽區",      "adcode": "370214",      "street": "和融路",      "street_number": "",      "direction": "",      "distance": ""    },    "pois": [],    "roads": [],    "poiRegions": [],    "sematic_description": "青島寶佳自動化設備有限公司北575米",    "cityCode": 236  }}}

 

=================================================================

以下內容轉自他人博客,返回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;
                    }
                    else
                        return "未獲取到位置信息,錯誤碼3";
                }
                else
                {
                    return "未獲取到位置信息,錯誤碼1";
                }
            }
            catch (System.Exception ex)
            {
                return "未獲取到位置信息,錯誤碼2";
            }
        }

 

url中的參數:

參數 是否必須 默認值 格式舉例 含義
coordtype bd09ll bd09ll 百度經緯度坐標 坐標的類型,目前支持的坐標類型包括:bd09ll(百度經緯度坐標)、gcj02ll(國測局經緯度坐標)、wgs84ll( GPS經緯度)
location 38.76623,116.43213 lat<緯度>,lng<經度> 根據經緯度坐標獲取地址
pois 0 0 是否顯示指定位置周邊的poi,0為不顯示,1為顯示。當值為1時,顯示周邊100米內的poi。

 

 

運行方法返回的結果:

北京市海淀區中關村大街27號1101-08室

 

從百度api返回的結果:

  <?xml version="1.0" encoding="utf-8" ?> 
- <GeocoderSearchResponse>
  <status>0</status> 
- <result>
- <location>
  <lat>39.983424051248</lat> 
  <lng>116.32298703399</lng> 
  </location>
  <formatted_address>北京市海淀區中關村大街27號1101-08室</formatted_address> 
  <business>中關村,人民大學,蘇州街</business> 
- <addressComponent>
  <streetNumber /> 
  <street>中關村大街</street> 
  <district>海淀區</district> 
  <city>北京市</city> 
  <province>北京市</province> 
  </addressComponent>
  <cityCode>131</cityCode> 
- <pois>
- <poi>
  <addr>中關村西區南側(中關村科技園區內)</addr> 
  <distance>0.050000</distance> 
  <name>中關村大廈</name> 
  <poiType>辦公大廈,商務大廈</poiType> 
  <tel>(010)82856666</tel> 
  <zip>100000</zip> 
- <point>
  <x>116.32298658484</x> 
  <y>39.983423843929</y> 
  </point>
  </poi>
- <poi>
  <addr>中關村大街27號</addr> 
  <distance>0.050000</distance> 
  <name>眉州東坡酒樓中關村店</name> 
  <poiType>中餐館,餐飲</poiType> 
  <tel>(010)82856948</tel> 
  <zip /> 
- <point>
  <x>116.32298658484</x> 
  <y>39.983423843929</y> 
  </point>
  </poi>
- <poi>
  <addr>中關村大街27號</addr> 
  <distance>0.050000</distance> 
  <name>中國人民財產保險中關村營業部</name> 
  <poiType>中國人民財產保險,保險公司,金融</poiType> 
  <tel>(010)82856779</tel> 
  <zip>100000</zip> 
- <point>
  <x>116.32298658484</x> 
  <y>39.983423843929</y> 
  </point>
  </poi>
- <poi>
  <addr>北京市海淀區</addr> 
  <distance>94.432081</distance> 
  <name>光合作用書房</name> 
  <poiType>圖書音像,購物</poiType> 
  <tel /> 
  <zip /> 
- <point>
  <x>116.32239334388</x> 
  <y>39.983890240676</y> 
  </point>
  </poi>
- <poi>
  <addr>中關村大街27號</addr> 
  <distance>42.195731</distance> 
  <name>建行中關村支行</name> 
  <poiType>中國建設銀行,銀行,金融</poiType> 
  <tel /> 
  <zip>100000</zip> 
- <point>
  <x>116.32292037972</x> 
  <y>39.983711118168</y> 
  </point>
  </poi>
- <poi>
  <addr>北京市海淀區</addr> 
  <distance>62.342644</distance> 
  <name>海淀醫院-激光整形美容部</name> 
  <poiType>美容美發,生活服務</poiType> 
  <tel /> 
  <zip /> 
- <point>
  <x>116.32317954086</x> 
  <y>39.98301950182</y> 
  </point>
  </poi>
- <poi>
  <addr>中關村大街19號新中關購物中心1樓</addr> 
  <distance>112.983688</distance> 
  <name>星巴克新中關店</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 返回結果狀態值, 成功返回0,其他值請查看附錄
location lat 緯度坐標
lng 經度坐標
formatted_address 結構化地址信息
business 所在商圈信息,如 "人民大學,中關村,蘇州街"
addressComponent city 城市名
district 區縣名
province 省名
street 街道名
street_number 街道門牌號
pois(周邊poi數組) addr 地址信息
cp 數據來源
distance 離坐標點距離
name poi名稱
poiType poi類型,如’ 辦公大廈,商務大廈’
point poi坐標{x,y}
tel 電話
uid poi唯一標識
zip 郵編

附錄:

返回碼 定義
0 正常
1 服務器內部錯誤
2 請求參數非法
3 權限校驗失敗
4 配額校驗失敗
5 ak不存在或者非法
101 服務禁用
102 不通過白名單或者安全碼不對
2xx 無權限
3xx 配額錯誤

 

 


免責聲明!

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



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