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 = ,
/// <summary>
/// 请求参数非法
/// </summary>
ParameterInvalid = ,
/// <summary>
/// 权限校验失败
/// </summary>
VerifyFailure = ,
/// <summary>
/// 配额校验失败
/// </summary>
QuotaFailure = ,
/// <summary>
/// 不存在或者非法
/// </summary>
AkFailure = ,
/// <summary>
/// Transform 内部错误
/// </summary>
InternalError = ,
/// <summary>
/// from非法
/// </summary>
FromIllegal = ,
/// <summary>
/// to非法
/// </summary>
ToIllegal = ,
/// <summary>
/// coords非法
/// </summary>
CoordsIllegal = ,
/// <summary>
/// coords个数非法,超过限制
/// </summary>
CoordsCountIllegal = }

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#封装百度Web服务API处理包含(Geocoding API,坐标转换API)的更多相关文章

  1. Node.js调用百度地图Web服务API的Geocoding接口进行点位反地理信息编码

    (从我的新浪博客上搬来的,做了一些修改.) 最近迷上了node.js以及JavaScript.现在接到一个活,要解析一个出租车点位数据的地理信息.于是就想到使用Node.js调用百度地图API进行解析 ...

  2. paper 88:人脸检测和识别的Web服务API

    本文汇总了全球范围内提供基于Web服务的人脸检测和识别的API,便于网络中快速部署和人脸相关的一些应用. 1:从How-old的火爆说起 最开始,网站的开发者只是给一个几百人的群发送email,请他们 ...

  3. 常见的三种Web服务架构

    常见的三种Web服务架构 转自http://www.cnblogs.com/bvbook/archive/2008/12/24/1360942.html 相互竞争的服务架构 The Competing ...

  4. C# 调用百度地图Web服务API

    最近公司项目中需要根据两个地点的交通路径和距离做一些数据推荐,为了程序的稳定和用户体验所以想从百度地图 API 采集数据保存到数据库中,经过一翻研究之后选定了百度地图 Web 服务 API 中的 Di ...

  5. C# 调用百度地图 Web 服务 API

    最近公司项目中需要根据两个地点的交通路径和距离做一些数据推荐,为了程序的稳定和用户体验所以想从百度地图 API 采集数据保存到数据库中,经过一翻研究之后选定了百度地图 Web 服务 API 中的 Di ...

  6. 提高生产力:发送邮件API和Web服务(包含源码)

    在Web开发中,发邮件是一种非常常见的功能或任务. 发送邮件的6种方式 一文提到了6种方法,文章发表后,有网友指出了还有另外一种方法,Ant中也可以发送邮件. 打开Foxmail之类的邮件客户端或者在 ...

  7. 通过 Jersey Http请求头,Http响应头,客户端 API 调用 REST 风格的 Web 服务

    原地址:http://blog.csdn.net/li575098618/article/details/47853263 Jersey 1.0 是一个开源的.可以用于生产环境的 JAX-RS(RES ...

  8. 前端利用百度开发文档给的web服务接口实现对某个区域周边配套的检索

    最近项目需要实现地图功能,以便于实现对房源周边配套设施的检索.内容如下 其实百度官方有对应的api,但是对于一个网站来说这样的样式难免有些难看 这样的结果显然不能满足当下的需求,所以我决定利用官方给的 ...

  9. BMap:WEB 服务API

    ylbtech-Map-Baidu: WEB 服务API 百度地图Web服务API为开发者提供http/https接口,即开发者通过http/https形式发起检索请求,获取返回json或xml格式的 ...

随机推荐

  1. 关于opacity的思考

    今天在封装图片轮播的插件的时候,产生了这个opacity的小小思考. 我这个轮播的思路不是以前baidu输入法官网的设置外层容器overflow为hidden,position为relative用se ...

  2. window.open()页面之间函数传值

    项目中遇到的问题,使用window.open()开一个页面之后,cookie会消失,所以无法一键切肤不管作用,解决方案如下: window.open()总结: window.open("sU ...

  3. hadoop fs:du & count统计hdfs文件(目录下文件)大小的用法

    hadoop fs 更多用法,请参考官网:http://hadoop.apache.org/docs/r1.0.4/cn/hdfs_shell.html 以下是我的使用hadoop fs -du统计文 ...

  4. Spring 在XML中声明切面/AOP

    在Spring的AOP配置命名空间中,我们能够找到声明式切面选择.看以下: <aop:config> <!-- AOP定义開始 --> <aop:pointcut/> ...

  5. 【Nodejs】“快算24”扑克牌游戏算法 1.02

    快算24是一种挺好的锻炼孩子算数能力的扑克牌游戏,它的游戏方式是把四张牌的牌面数值通过有限四则运算得到结果24,四张牌必须仅用一次.各地玩法还有点差别,有的只算1-10,其它抽出来:有的地方把整幅牌都 ...

  6. Cocos2d-x 3.0多线程异步资源载入代码

    // AppDelegate.cpp bool AppDelegate::applicationDidFinishLaunching() { - - FlashScene* scene = Flash ...

  7. DevExpress学习01——下载与安装

    记得刚接触编程时,虽然实现了功能,但用户界面十分丑陋,老师叫我们美化一下界面,不要千篇一律,当时觉得能够写出来功能就洋洋得意了,不觉得界面丑陋.后来,在程序比赛中,我接触了一种第三方控件,它可以快速实 ...

  8. 安装使用ionic3

    1.安装ionic3 $ npm install -g ionic@latest 2.创建ionic3项目 $ ionic start myNewProject blank 3.启动ionic3项目 ...

  9. IIS 之 连接数、并发连接数、最大并发工作线程数、队列长度、最大工作进程数

    一.IIS连接数 一般购买过虚拟主机的朋友都熟悉购买时,会限制IIS连接数,顾名思义即为IIS服务器可以同时容纳客户请求的最高连接数,准确的说应该叫“IIS限制连接数”. 客户请求的连接内容包括: [ ...

  10. shell中如何取括号中的字符

    1. 使用grep(结果带括号,不知道有没有办法仅把括号中的内容匹配出来) $a='abc[edg]adfirpqu' $echo $a|grep -o '\[.*\]' #中括号的处理需要转义 [e ...