当前位置:  编程技术>.net/c#/asp.net

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

    来源: 互联网  发布时间:2014-10-29

    本文导语:  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";        publi...

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

    }

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

代码如下:

public class BaiduGeocodingResults
    {
        ///
        /// 返回结果状态值, 成功返回0,其他值请查看附录。
        ///
        [JsonProperty(PropertyName = "status")]
        public Status Status;

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

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

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

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

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

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

    public class BaiduGeocodingAddress
    {
        ///
        /// 城市名
        ///
        [JsonProperty(PropertyName = "city")]
        public string City;
        ///
        /// 区县名
        ///
        [JsonProperty(PropertyName = "district")]
        public string District;
        ///
        /// 省名
        ///
        [JsonProperty(PropertyName = "province")]
        public string Province;
        ///
        /// 街道名
        ///
        [JsonProperty(PropertyName = "street")]
        public string Street;
        ///
        /// 街道门牌号
        ///
        [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;
        }

        ///
        /// 请求
        ///
        ///
        ///
        ///
        ///
        public void Request(string param, BaiduLbsType baiduLbsType, Encoding encoding, Action 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));
        }

        ///
        /// 响应
        ///
        ///
        ///
        ///
        ///
        public T Response(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(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(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(request, BaiduLbsType.GeocodingApIv2, Encoding.UTF8);
        }

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

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


    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • ticpp(TinyXML++)TinyXML的C++封装介绍
  • 封装libxml2,那位有经验,请进
  • php通过pack和unpack函数实现对二进制数据封装及解析
  • 函数库封装问题!谢谢了
  • IP报文头的重新封装
  • Linux下的JNI封装 jniexec
  • OpenCL 封装库 CLOGS
  • ALSA库的C++封装 aseqmm
  • PHP的Session封装 Zebra_Session
  • Properties 文件封装库 OWNER
  • DirectX 的封装库 SharpDX
  • Xlib函数调用的封装 GDK
  • AMR-WB封装库 amrwb
  • VC的数据库访问封装库 exdb
  • readline 封装程序 rlwrap
  • AMR封装库 amrnb
  • Windows API 的封装包 WE
  • OpenGL的C++封装 D'Enfent Engine
  • Windows组件封装接口 STLSoft
  • WebKit的QT封装 QtWebKit
  • PHP的SQL封装脚本 TbsSQL


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3