欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-23 09:25:40
1、创建基础参数类 复制代码 代码如下:public static class baiduconstparams    { &nb...

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);
        }
    }