C#封装百度Web服务API处理包含(Geocoding API,坐标转换API)

1、创建基础参数类

C#封装百度Web服务API处理包含(Geocoding API,坐标转换API)
 1   public static class BaiduConstParams
 2     {
 3         public const string PlaceApIv2Search = "http://api.map.baidu.com/place/v2/search";
 4         public const string PlaceApIv2Detail = "http://api.map.baidu.com/place/v2/detail";
 5         public const string PlaceApIv2Eventsearch = "http://api.map.baidu.com/place/v2/eventsearch";
 6         public const string PlaceApIv2Eventdetail = "http://api.map.baidu.com/place/v2/eventdetail";
 7         public const string GeocodingApIv2 = "http://api.map.baidu.com/geocoder/v2/";
 8         public const string GeocodingApIv2Reverse = "http://api.map.baidu.com/geocoder/v2/";
 9         public const string TranslateApi = "http://openapi.baidu.com/public/2.0/bmt/translate";
10         public const string GeoconvApi = "http://api.map.baidu.com/geoconv/v1/";
11     }
12 
13     public static class BaiduErrorMessages
14     {
15         public const string NotKey = "密钥不存在";
16         public const string LackParam = "缺少必要请求参数";
17     }
View Code

2、定义API错误信息与产品信息

C#封装百度Web服务API处理包含(Geocoding API,坐标转换API)
 1 public enum BaiduLbsType
 2     {
 3         PlaceApIv2Search,
 4         PlaceApIv2Detail,
 5         PlaceApIv2Eventsearch,
 6         PlaceApIv2Eventdetail,
 7         GeocodingApIv2,
 8         GeocodingApIv2Reverse,
 9         Translate,
10         Geoconv
11     }
12 
13     public enum Status
14     {
15         /// <summary>
16         /// 正常    
17         /// </summary>
18         Ok = 0,
19         /// <summary>
20         /// 请求参数非法    
21         /// </summary>
22         ParameterInvalid = 2,
23         /// <summary>
24         /// 权限校验失败
25         /// </summary>
26         VerifyFailure = 3,
27         /// <summary>
28         /// 配额校验失败
29         /// </summary>
30         QuotaFailure = 4,
31         /// <summary>
32         /// 不存在或者非法    
33         /// </summary>
34         AkFailure = 5,
35         /// <summary>
36         /// Transform 内部错误
37         /// </summary>
38         InternalError = 1,
39         /// <summary>
40         /// from非法
41         /// </summary>
42         FromIllegal = 21,
43         /// <summary>
44         /// to非法
45         /// </summary>
46         ToIllegal = 22,
47         /// <summary>
48         /// coords非法
49         /// </summary>
50         CoordsIllegal = 24,
51         /// <summary>
52         /// coords个数非法,超过限制
53         /// </summary>        
54         CoordsCountIllegal = 25
55 
56     }
View Code

3、定义API结果返回实体映射类

C#封装百度Web服务API处理包含(Geocoding API,坐标转换API)
 public class BaiduGeocodingResults
    {
        /// <summary>
        /// 返回结果状态值, 成功返回0,其他值请查看附录。
        /// </summary>
        [JsonProperty(PropertyName = "status")]
        public Status Status;

        /// <summary>
        /// 返回结果状态值, 成功返回0,其他值请查看附录。
        /// </summary>
        [JsonProperty(PropertyName = "result")]
        public BaiduGeocodingResult Result;
    }

    public class BaiduGeocodingResult
    {
        /// <summary>
        /// 经纬度坐标
        /// </summary>
        [JsonProperty(PropertyName = "location")]
        public BaiduGeocodingLoaction Location;
        /// <summary>
        /// 位置的附加信息,是否精确查找。1为精确查找,0为不精确。
        /// </summary>
        [JsonProperty(PropertyName = "precise")]
        public int Precise;
        /// <summary>
        /// 可信度
        /// </summary>
        [JsonProperty(PropertyName = "confidence")]
        public int Confidence;
        /// <summary>
        /// 地址类型
        /// </summary>
        [JsonProperty(PropertyName = "level")]
        public string Level;

        /// <summary>
        /// 结构化地址信息
        /// </summary>
        [JsonProperty(PropertyName = "formatted_address")]
        public string FormattedAddress;

        /// <summary>
        /// 所在商圈信息,如 "人民大学,中关村,苏州街"
        /// </summary>
        [JsonProperty(PropertyName = "business")]
        public string Business;

        /// <summary>
        /// 具体地址
        /// </summary>
        [JsonProperty(PropertyName = "addressComponent")]
        public BaiduGeocodingAddress AddressComponent;
    }

    public class BaiduGeocodingLoaction
    {
        /// <summary>
        /// 纬度值
        /// </summary>
        [JsonProperty(PropertyName = "lat")]
        public decimal Lat;
        /// <summary>
        /// 经度值
        /// </summary>
        [JsonProperty(PropertyName = "lng")]
        public decimal Lng;
    }

    public class BaiduGeocodingAddress
    {
        /// <summary>
        /// 城市名
        /// </summary>
        [JsonProperty(PropertyName = "city")]
        public string City;
        /// <summary>
        /// 区县名
        /// </summary>
        [JsonProperty(PropertyName = "district")]
        public string District;
        /// <summary>
        /// 省名
        /// </summary>
        [JsonProperty(PropertyName = "province")]
        public string Province;
        /// <summary>
        /// 街道名
        /// </summary>
        [JsonProperty(PropertyName = "street")]
        public string Street;
        /// <summary>
        /// 街道门牌号
        /// </summary>
        [JsonProperty(PropertyName = "street_number")]
        public string StreetNumber;
    }
View Code

4、创建API通用处理类

C#封装百度Web服务API处理包含(Geocoding API,坐标转换API)
  1 public class BaiduLbs
  2     {
  3         private readonly string _key;
  4 
  5         public static string CurrentRequest = "";
  6 
  7         public BaiduLbs(string key)
  8         {
  9             _key = key;
 10         }
 11 
 12         /// <summary>
 13         /// 请求
 14         /// </summary>
 15         /// <param name="param"></param>
 16         /// <param name="baiduLbsType"></param>
 17         /// <param name="encoding"></param>
 18         /// <param name="action"></param>
 19         public void Request(string param, BaiduLbsType baiduLbsType, Encoding encoding, Action<string> action)
 20         {
 21             WebClient webClient = new WebClient { Encoding = encoding };
 22             string url = "";
 23             switch (baiduLbsType)
 24             {
 25                 case BaiduLbsType.PlaceApIv2Search:
 26                     url = string.Format(BaiduConstParams.PlaceApIv2Search + "?{0}", param);
 27                     break;
 28                 case BaiduLbsType.PlaceApIv2Detail:
 29                     url = string.Format(BaiduConstParams.PlaceApIv2Detail + "?{0}", param);
 30                     break;
 31                 case BaiduLbsType.PlaceApIv2Eventsearch:
 32                     url = string.Format(BaiduConstParams.PlaceApIv2Eventsearch + "?{0}", param);
 33                     break;
 34                 case BaiduLbsType.PlaceApIv2Eventdetail:
 35                     url = string.Format(BaiduConstParams.PlaceApIv2Eventdetail + "?{0}", param);
 36                     break;
 37                 case BaiduLbsType.GeocodingApIv2:
 38                 case BaiduLbsType.GeocodingApIv2Reverse:
 39                     url = string.Format(BaiduConstParams.GeocodingApIv2 + "?{0}", param);
 40                     break;
 41                 case BaiduLbsType.Translate:
 42                     url = string.Format(BaiduConstParams.TranslateApi + "?{0}", param);
 43                     break;
 44                 case BaiduLbsType.Geoconv:
 45                     url = string.Format(BaiduConstParams.GeoconvApi + "?{0}", param);
 46                     break;
 47 
 48 
 49             }
 50             CurrentRequest = url;
 51             action(webClient.DownloadString(url));
 52         }
 53 
 54         /// <summary>
 55         /// 响应
 56         /// </summary>
 57         /// <typeparam name="T"></typeparam>
 58         /// <param name="param"></param>
 59         /// <param name="baiduLbsType"></param>
 60         /// <param name="encoding"></param>
 61         public T Response<T>(string param, BaiduLbsType baiduLbsType, Encoding encoding)
 62         {
 63             T t = default(T);
 64 
 65             Request(param, baiduLbsType, encoding, json =>
 66                 {
 67                     if (baiduLbsType == BaiduLbsType.GeocodingApIv2 || baiduLbsType == BaiduLbsType.GeocodingApIv2Reverse)
 68                     {
 69                         if (json.Contains("\"result\":[]"))
 70                         {
 71                             json = json.Replace("\"result\":[]", "\"result\":{}");
 72                         }
 73                     }
 74                     t = (T)JsonConvert.DeserializeObject(json, typeof(T));
 75                 });
 76             return t;
 77         }
 78 
 79         public BaiduGeocodingResults BaiduGeocoding(string address, string city)
 80         {
 81             address = System.Web.HttpUtility.UrlEncode(address);
 82             city = System.Web.HttpUtility.UrlEncode(city);
 83             string request = string.Format("address={0}&output=json&ak={1}&city={2}", address, _key, city);
 84             var result = Response<BaiduGeocodingResults>(request, BaiduLbsType.GeocodingApIv2, Encoding.UTF8);
 85             if (result.Status == Status.Ok && result.Result.Location == null)
 86             {
 87                 request = string.Format("address={0}&output=json&ak={1}&city={2}", city + address, _key, city);
 88                 return Response<BaiduGeocodingResults>(request, BaiduLbsType.GeocodingApIv2, Encoding.UTF8);
 89             }
 90             return result;
 91         }
 92 
 93         public BaiduGeocodingResults BaiduGeocoding(string longitude, string dimensions, string pois)
 94         {
 95             var location = longitude + "," + dimensions;
 96             string request = string.Format("ak={0}&location={1}&pois={2}", _key, location, pois);
 97             return Response<BaiduGeocodingResults>(request, BaiduLbsType.GeocodingApIv2, Encoding.UTF8);
 98         }
 99 
100       
101         public GeoconvResults BaiduGeoconv(GeoconvParams geoconvParams, ref List<GeoconvPOI> geoconvPois)
102         {
103             geoconvParams.Ak = _key;
104             return Response<GeoconvResults>(geoconvParams.ToString(ref geoconvPois), BaiduLbsType.Geoconv, Encoding.UTF8);
105         }
106 
107         public GeoconvResults BaiduGeoconv(GeoconvParams geoconvParams, GeoconvPOI geoconvPoi)
108         {
109             geoconvParams.Ak = _key;
110             List<GeoconvPOI> geoconvPois = new List<GeoconvPOI>
111                                                {
112                                                    geoconvPoi
113                                                };
114             return Response<GeoconvResults>(geoconvParams.ToString(ref geoconvPois), BaiduLbsType.Geoconv, Encoding.UTF8);
115         }
116     }
View Code

 

C#封装百度Web服务API处理包含(Geocoding API,坐标转换API),布布扣,bubuko.com

C#封装百度Web服务API处理包含(Geocoding API,坐标转换API)

上一篇:在WINDOWS下对LINUX中PHP环境的根目录进行共享


下一篇:c# 判断窗体是否永在最前(TopMost),调用windows API