c#封装百度web服务geocoding api 、百度坐标转换示例

 更新时间:2014年04月07日 15:59:58   转载 作者:  
这篇文章主要介绍了c#封装百度Web服务geocoding api 、百度坐标转换,需要的朋友可以参考下

1、创建基础参数类

复制代码 代码如下:

public static class BaiduConstParams
    {
        public const string PlaceApIv2Search = "http://api.map.baidu.com/place/v2/search";
        public const string PlaceApIv2Detail = "http://api.map.baidu.com/place/v2/detail";
        public const string PlaceApIv2Eventsearch = "http://api.map.baidu.com/place/v2/eventsearch";
        public const string PlaceApIv2Eventdetail = "http://api.map.baidu.com/place/v2/eventdetail";
        public const string GeocodingApIv2 = "http://api.map.baidu.com/geocoder/v2/";
        public const string GeocodingApIv2Reverse = "http://api.map.baidu.com/geocoder/v2/";
        public const string TranslateApi = "http://openapi.baidu.com/public/2.0/bmt/translate";
        public const string GeoconvApi = "http://api.map.baidu.com/geoconv/v1/";
    }

    public static class BaiduErrorMessages
    {
        public const string NotKey = "密钥不存在";
        public const string LackParam = "缺少必要请求参数";
    }

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

复制代码 代码如下:

public enum BaiduLbsType
    {
        PlaceApIv2Search,
        PlaceApIv2Detail,
        PlaceApIv2Eventsearch,
        PlaceApIv2Eventdetail,
        GeocodingApIv2,
        GeocodingApIv2Reverse,
        Translate,
        Geoconv
    }

    public enum Status
    {
        /// <summary>
        /// 正常   
        /// </summary>
        Ok = 0,
        /// <summary>
        /// 请求参数非法   
        /// </summary>
        ParameterInvalid = 2,
        /// <summary>
        /// 权限校验失败
        /// </summary>
        VerifyFailure = 3,
        /// <summary>
        /// 配额校验失败
        /// </summary>
        QuotaFailure = 4,
        /// <summary>
        /// 不存在或者非法   
        /// </summary>
        AkFailure = 5,
        /// <summary>
        /// Transform 内部错误
        /// </summary>
        InternalError = 1,
        /// <summary>
        /// from非法
        /// </summary>
        FromIllegal = 21,
        /// <summary>
        /// to非法
        /// </summary>
        ToIllegal = 22,
        /// <summary>
        /// coords非法
        /// </summary>
        CoordsIllegal = 24,
        /// <summary>
        /// coords个数非法,超过限制
        /// </summary>       
        CoordsCountIllegal = 25

    }

3、定义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;
    }

4、创建API通用处理类

复制代码 代码如下:

public class BaiduLbs
    {
        private readonly string _key;

        public static string CurrentRequest = "";

        public BaiduLbs(string key)
        {
            _key = key;
        }

        /// <summary>
        /// 请求
        /// </summary>
        /// <param name="param"></param>
        /// <param name="baiduLbsType"></param>
        /// <param name="encoding"></param>
        /// <param name="action"></param>
        public void Request(string param, BaiduLbsType baiduLbsType, Encoding encoding, Action<string> action)
        {
            WebClient webClient = new WebClient { Encoding = encoding };
            string url = "";
            switch (baiduLbsType)
            {
                case BaiduLbsType.PlaceApIv2Search:
                    url = string.Format(BaiduConstParams.PlaceApIv2Search + "?{0}", param);
                    break;
                case BaiduLbsType.PlaceApIv2Detail:
                    url = string.Format(BaiduConstParams.PlaceApIv2Detail + "?{0}", param);
                    break;
                case BaiduLbsType.PlaceApIv2Eventsearch:
                    url = string.Format(BaiduConstParams.PlaceApIv2Eventsearch + "?{0}", param);
                    break;
                case BaiduLbsType.PlaceApIv2Eventdetail:
                    url = string.Format(BaiduConstParams.PlaceApIv2Eventdetail + "?{0}", param);
                    break;
                case BaiduLbsType.GeocodingApIv2:
                case BaiduLbsType.GeocodingApIv2Reverse:
                    url = string.Format(BaiduConstParams.GeocodingApIv2 + "?{0}", param);
                    break;
                case BaiduLbsType.Translate:
                    url = string.Format(BaiduConstParams.TranslateApi + "?{0}", param);
                    break;
                case BaiduLbsType.Geoconv:
                    url = string.Format(BaiduConstParams.GeoconvApi + "?{0}", param);
                    break;


            }
            CurrentRequest = url;
            action(webClient.DownloadString(url));
        }

        /// <summary>
        /// 响应
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="param"></param>
        /// <param name="baiduLbsType"></param>
        /// <param name="encoding"></param>
        public T Response<T>(string param, BaiduLbsType baiduLbsType, Encoding encoding)
        {
            T t = default(T);

            Request(param, baiduLbsType, encoding, json =>
                {
                    if (baiduLbsType == BaiduLbsType.GeocodingApIv2 || baiduLbsType == BaiduLbsType.GeocodingApIv2Reverse)
                    {
                        if (json.Contains("\"result\":[]"))
                        {
                            json = json.Replace("\"result\":[]", "\"result\":{}");
                        }
                    }
                    t = (T)JsonConvert.DeserializeObject(json, typeof(T));
                });
            return t;
        }

        public BaiduGeocodingResults BaiduGeocoding(string address, string city)
        {
            address = System.Web.HttpUtility.UrlEncode(address);
            city = System.Web.HttpUtility.UrlEncode(city);
            string request = string.Format("address={0}&output=json&ak={1}&city={2}", address, _key, city);
            var result = Response<BaiduGeocodingResults>(request, BaiduLbsType.GeocodingApIv2, Encoding.UTF8);
            if (result.Status == Status.Ok && result.Result.Location == null)
            {
                request = string.Format("address={0}&output=json&ak={1}&city={2}", city + address, _key, city);
                return Response<BaiduGeocodingResults>(request, BaiduLbsType.GeocodingApIv2, Encoding.UTF8);
            }
            return result;
        }

        public BaiduGeocodingResults BaiduGeocoding(string longitude, string dimensions, string pois)
        {
            var location = longitude + "," + dimensions;
            string request = string.Format("ak={0}&location={1}&pois={2}", _key, location, pois);
            return Response<BaiduGeocodingResults>(request, BaiduLbsType.GeocodingApIv2, Encoding.UTF8);
        }

     
        public GeoconvResults BaiduGeoconv(GeoconvParams geoconvParams, ref List<GeoconvPOI> geoconvPois)
        {
            geoconvParams.Ak = _key;
            return Response<GeoconvResults>(geoconvParams.ToString(ref geoconvPois), BaiduLbsType.Geoconv, Encoding.UTF8);
        }

        public GeoconvResults BaiduGeoconv(GeoconvParams geoconvParams, GeoconvPOI geoconvPoi)
        {
            geoconvParams.Ak = _key;
            List<GeoconvPOI> geoconvPois = new List<GeoconvPOI>
                                               {
                                                   geoconvPoi
                                               };
            return Response<GeoconvResults>(geoconvParams.ToString(ref geoconvPois), BaiduLbsType.Geoconv, Encoding.UTF8);
        }
    }

相关文章

  • C#实现的字符串转MD5码函数实例

    C#实现的字符串转MD5码函数实例

    这篇文章主要介绍了C#实现的字符串转MD5码函数,结合简单实例形式分析了C#字符串的转换、遍历、加密等操作技巧,需要的朋友可以参考下
    2016-07-07
  • c# JSON返回格式的WEB SERVICE

    c# JSON返回格式的WEB SERVICE

    首先用c#创建一个web service,主要是利用其WSDL的功能,当然也可以利用php创建一个,道理都是一样的
    2008-12-12
  • C#中接口(interface)的理解

    C#中接口(interface)的理解

    C#中接口(interface)的理解...
    2007-03-03
  • C#定时器实现自动执行的方法

    C#定时器实现自动执行的方法

    这篇文章主要介绍了C#定时器实现自动执行的方法,实例分析了C#定时器参数的设置及方法的调用与实现,需要的朋友可以参考下
    2015-01-01
  • c#日志记录帮助类分享

    c#日志记录帮助类分享

    这篇文章主要介绍了c#日志记录帮助类,可以设置记录的日志类型,需要的朋友可以参考下
    2014-03-03
  • C#中使用UDP通信实例

    C#中使用UDP通信实例

    这篇文章主要介绍了C#中使用UDP通信实例,非常实用的技巧,需要的朋友可以参考下
    2014-08-08
  • C#实现文件夹的复制和删除

    C#实现文件夹的复制和删除

    这篇文章主要为大家详细介绍了C#实现文件夹的复制和删除功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • C#如何将DataTable导出到Excel解决方案

    C#如何将DataTable导出到Excel解决方案

    由于公司项目中需要将系统内用户操作的所有日志进行转存备份,考虑到以后可能还需要还原,所以最后决定将日志数据备份到Excel中
    2012-11-11
  • 一个状态机的实现

    一个状态机的实现

    本文主要介绍了C#实现一个状态机的思路与方法,具有很好的参考价值,下面跟着小编一起来看下吧
    2017-02-02
  • C#字体池技术实现代码详解

    C#字体池技术实现代码详解

    在本篇文章里小编给大家整理的是关于C#字体池技术实现代码详解内容,有需要的朋友们可以学习下。
    2019-11-11

最新评论