C#的百度地圖開發(三)依據坐標獲取位置、商圈及周邊信息


我們得到了百度坐標,現在依據這一坐標來獲取相應的信息。下面是相應的代碼

 

[html]  view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. public class BaiduMap  
  2. {  
  3.         /// <summary>  
  4.         /// 依據坐標獲取定位信息的URL模板。  
  5.         /// 參數1:百度地圖API的KEY。  
  6.         /// 參數2:坐標(經度,緯度)。          
  7.         /// </summary>  
  8.         public const string GEOCODING_COORDINATE_URL_TEMPLATE =  
  9.             "http://api.map.baidu.com/geocoder/v2/?ak={0}&location={1}&output=json&pois=1";  
  10.   
  11.         /// <summary>  
  12.         /// 依據坐標獲取定位信息  
  13.         /// </summary>  
  14.         /// <param name="coordinates">坐標(經度,緯度),多個坐標間用分號隔開</param>  
  15.         /// <param name="mapCoordinateType">坐標類型</param>  
  16.         /// <returns></returns>  
  17.         public static CoordLocationResult FetchLocation(String coordinates,  
  18.                                                      MapCoordinateType mapCoordinateType)  
  19.         {  
  20.             CoordTransResult transformResult = TransToBaiduCoord(coordinates, mapCoordinateType);  
  21.             String info = "";  
  22.             if (!transformResult.status.Equals(CoordTransStatus.OK))  
  23.             {  
  24.                 info = "坐標轉換異常:狀態是---" + transformResult.status.ToString();  
  25.                 return null;  
  26.             }  
  27.   
  28.             if (transformResult.result == null || transformResult.result.Length <= 0)  
  29.             {  
  30.                 info = "坐標轉換異常:結果為空或數組長度為0";  
  31.                 return null;  
  32.             }  
  33.   
  34.             String locationUrl = "";  
  35.             foreach (Coordinate coordTemp in transformResult.result)  
  36.             {  
  37.                 locationUrl = String.Format(GEOCODING_COORDINATE_URL_TEMPLATE,  
  38.                                                     MAP_KEY_BAI_DU,  
  39.                                                     coordTemp.x + "," + coordTemp.y);  
  40.             }  
  41.   
  42.             String locationResponseText = RequestHelper.RequestUrl(locationUrl, null);  
  43.   
  44.             CoordLocationResult locationResult = null;  
  45.             try  
  46.             {  
  47.                 locationResult = Newtonsoft.Json.JsonConvert.DeserializeObject<CoordLocationResult>(locationResponseText);  
  48.             }  
  49.             catch (Exception e)  
  50.             {  
  51.                 info = "定位異常:" + e.Message;  
  52.                 return null;  
  53.             }  
  54.   
  55.             return locationResult;  
  56.         }  
  57. }  

注:

 

(1).使用const常量來定義一個百度地圖API的URL模板,方便后面的調用。

(2).TransToBaiduCoord函數是《C#的百度地圖開發(二)轉換JSON數據為相應的類》中將非百度坐標轉換成百度坐標方法的封裝。

(3).RequestUrl方法是《C#的百度地圖開發(一)發起HTTP請求》所說的發起HTTP請求的封裝。

(4).CoordLocationResult類的具體實現,請參看后面的代碼。

 

[html]  view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. namespace MapApi.Baidu  
  2. {  
  3.   
  4.     [Serializable]  
  5.     public class CoordLocationResult  
  6.     {  
  7.         /// <summary>  
  8.         /// 狀態  
  9.         /// </summary>  
  10.         public String status { get; set; }  
  11.   
  12.         /// <summary>  
  13.         /// 結果  
  14.         /// </summary>  
  15.         public CoordLocationResult_Result result { get; set; }  
  16.     }  
  17.   
  18.     #region CoordLocationResult_Result  
  19.     /// <summary>  
  20.     /// 定位結果  
  21.     /// </summary>  
  22.     [Serializable]  
  23.     public class CoordLocationResult_Result  
  24.     {  
  25.         /// <summary>  
  26.         /// 定位的經度與緯度  
  27.         /// </summary>  
  28.         public CoordLocationResult_Result_Location location { get; set; }  
  29.   
  30.         /// <summary>  
  31.         /// 結構化地址信息  
  32.         /// </summary>  
  33.         public String formatted_address { get; set; }  
  34.   
  35.         /// <summary>  
  36.         /// 所在商圈信息,如 "人民大學,中關村,蘇州街"  
  37.         /// </summary>      
  38.         public String business { get; set; }  
  39.   
  40.         /// <summary>  
  41.         /// 定位的行政區域  
  42.         /// </summary>  
  43.         public CoordLocationResult_Result_AddressComponent addressComponent { get; set; }  
  44.   
  45.         /// <summary>  
  46.         /// 周邊位置  
  47.         /// </summary>  
  48.         public CoordLocationResult_Result_Poi[] pois { get; set; }  
  49.   
  50.         /// <summary>  
  51.         /// 周邊區域  
  52.         /// </summary>  
  53.         public CoordLocationResult_Result_PoiRegion[] poiRegions { get; set; }  
  54.   
  55.         /// <summary>  
  56.         /// 城市代碼  
  57.         /// </summary>  
  58.         public String cityCode { get; set; }  
  59.     }  
  60.   
  61.     /// <summary>  
  62.     /// 定位結果之定位的經緯度  
  63.     /// </summary>  
  64.     [Serializable]  
  65.     public class CoordLocationResult_Result_Location  
  66.     {  
  67.         /// <summary>  
  68.         /// 經度  
  69.         /// </summary>  
  70.         public String lng { get; set; }  
  71.   
  72.         /// <summary>  
  73.         /// 緯度  
  74.         /// </summary>  
  75.         public String lat { get; set; }  
  76.     }  
  77.   
  78.     /// <summary>  
  79.     /// 定位結果之行政區域  
  80.     /// </summary>  
  81.     [Serializable]  
  82.     public class CoordLocationResult_Result_AddressComponent  
  83.     {  
  84.         /// <summary>  
  85.         /// 城市名  
  86.         /// </summary>  
  87.         public String city { get; set; }  
  88.   
  89.         /// <summary>  
  90.         /// 區縣名  
  91.         /// </summary>  
  92.         public String district { get; set; }  
  93.   
  94.         /// <summary>  
  95.         /// 省名  
  96.         /// </summary>  
  97.         public String province { get; set; }  
  98.   
  99.         /// <summary>  
  100.         /// 街道名  
  101.         /// </summary>  
  102.         public String street { get; set; }  
  103.   
  104.         /// <summary>  
  105.         /// 街道門牌號  
  106.         /// </summary>  
  107.         public String street_number { get; set; }  
  108.     }  
  109.   
  110.     #endregion  
  111.   
  112.     #region CoordLocationResult_Result_Poi  
  113.     /// <summary>  
  114.     /// 周邊位置信息  
  115.     /// </summary>  
  116.     [Serializable]  
  117.     public class CoordLocationResult_Result_Poi  
  118.     {  
  119.         //"addr": "福建省廈門市湖里區嘉禾路388",  
  120.         //       "cp": "NavInfo",  
  121.         //       "direction": "西",  
  122.         //       "distance": "49",  
  123.         //       "name": "永同昌大廈",  
  124.         //       "poiType": "商務大廈",  
  125.         //       "point": {  
  126.         //           "x": 118.13374113945,  
  127.         //           "y": 24.501871673827  
  128.         //       },  
  129.         //       "tel": "",  
  130.         //       "uid": "19c4b3f2642893beafb22a1e",  
  131.         //       "zip": ""  
  132.   
  133.         /// <summary>  
  134.         /// 地址信息  
  135.         /// </summary>  
  136.         public String addr { get; set; }  
  137.   
  138.         /// <summary>  
  139.         /// 數據來源  
  140.         /// </summary>  
  141.         public String cp { get; set; }  
  142.   
  143.         /// <summary>  
  144.         /// 方向  
  145.         /// </summary>  
  146.         public String direction { get; set; }  
  147.   
  148.         /// <summary>  
  149.         /// 離坐標點距離  
  150.         /// </summary>  
  151.         public String distance { get; set; }  
  152.   
  153.         /// <summary>  
  154.         /// poi名稱  
  155.         /// </summary>  
  156.         public String name { get; set; }  
  157.   
  158.         /// <summary>  
  159.         /// poi類型,如’辦公大廈,商務大廈’  
  160.         /// </summary>  
  161.         public String poiType { get; set; }  
  162.   
  163.         /// <summary>  
  164.         /// poi坐標{x,y}  
  165.         /// </summary>  
  166.         public Coordinate point { get; set; }  
  167.   
  168.         /// <summary>  
  169.         /// 電話  
  170.         /// </summary>  
  171.         public String tel { get; set; }  
  172.   
  173.         /// <summary>  
  174.         /// poi唯一標識  
  175.         /// </summary>  
  176.         public String uid { get; set; }  
  177.   
  178.         /// <summary>  
  179.         /// 郵編  
  180.         /// </summary>  
  181.         public String zip { get; set; }  
  182.     }  
  183.     #endregion  
  184.   
  185.     #region CoordLocationResult_Result_PoiRegion  
  186.     /// <summary>  
  187.     /// 周邊區域  
  188.     /// </summary>  
  189.     [Serializable]  
  190.     public class CoordLocationResult_Result_PoiRegion  
  191.     {  
  192.         /// <summary>  
  193.         /// 目標方向。比如:內  
  194.         /// </summary>  
  195.         public String direction_desc { get; set; }  
  196.   
  197.         /// <summary>  
  198.         /// 區域名稱。比如:音樂·家生活廣場  
  199.         /// </summary>  
  200.         public String name { get; set; }  
  201.     }  
  202.     #endregion  
  203.   
  204. }  

注:類的構造方法依據前面所說的構造,也可以使用工具直接生成(鏈接)。

 

下面是測試代碼

 

[html]  view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. protected void btnTest_Click(object sender, EventArgs e)  
  2.        {            
  3.            Coordinate coordinate = new Coordinate("39.92", "116.46");  
  4.            CoordLocationResult coordLocationResult=BaiduMap.FetchLocation(coordinate);  
  5.            Alert.Show(coordLocationResult.status.ToString());  
  6.        }  


測試結果如下

 

從圖中可以看到,formatted_address是位置信息,business是商圈信息,pois是周圍的信息,其他的信息可自行參考百度地圖WebApi的官方文檔說明。

這樣,我們就得到了指定坐標點的位置信息,那得到了這些信息后,如果在前面的地圖上顯示呢?


免責聲明!

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



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