·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> C#的百度地图开发(三)依据坐标获取位置、商圈及周边信息

C#的百度地图开发(三)依据坐标获取位置、商圈及周边信息

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

我们得到了百度坐标,现在依据这一坐标来获取相应的信息。下面是相应的代码

 

[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的官方文档说明。

这样,我们就得到了指定坐标点的位置信息,那得到了这些信息后,如果在前面的地图上显示呢?