C#封装百度Web服务API处理包含(Geocoding API,坐标转换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 = ,
/// <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)的更多相关文章
- Node.js调用百度地图Web服务API的Geocoding接口进行点位反地理信息编码
		(从我的新浪博客上搬来的,做了一些修改.) 最近迷上了node.js以及JavaScript.现在接到一个活,要解析一个出租车点位数据的地理信息.于是就想到使用Node.js调用百度地图API进行解析 ... 
- paper 88:人脸检测和识别的Web服务API
		本文汇总了全球范围内提供基于Web服务的人脸检测和识别的API,便于网络中快速部署和人脸相关的一些应用. 1:从How-old的火爆说起 最开始,网站的开发者只是给一个几百人的群发送email,请他们 ... 
- 常见的三种Web服务架构
		常见的三种Web服务架构 转自http://www.cnblogs.com/bvbook/archive/2008/12/24/1360942.html 相互竞争的服务架构 The Competing ... 
- C# 调用百度地图Web服务API
		最近公司项目中需要根据两个地点的交通路径和距离做一些数据推荐,为了程序的稳定和用户体验所以想从百度地图 API 采集数据保存到数据库中,经过一翻研究之后选定了百度地图 Web 服务 API 中的 Di ... 
- C# 调用百度地图 Web 服务 API
		最近公司项目中需要根据两个地点的交通路径和距离做一些数据推荐,为了程序的稳定和用户体验所以想从百度地图 API 采集数据保存到数据库中,经过一翻研究之后选定了百度地图 Web 服务 API 中的 Di ... 
- 提高生产力:发送邮件API和Web服务(包含源码)
		在Web开发中,发邮件是一种非常常见的功能或任务. 发送邮件的6种方式 一文提到了6种方法,文章发表后,有网友指出了还有另外一种方法,Ant中也可以发送邮件. 打开Foxmail之类的邮件客户端或者在 ... 
- 通过 Jersey Http请求头,Http响应头,客户端 API 调用 REST 风格的 Web 服务
		原地址:http://blog.csdn.net/li575098618/article/details/47853263 Jersey 1.0 是一个开源的.可以用于生产环境的 JAX-RS(RES ... 
- 前端利用百度开发文档给的web服务接口实现对某个区域周边配套的检索
		最近项目需要实现地图功能,以便于实现对房源周边配套设施的检索.内容如下 其实百度官方有对应的api,但是对于一个网站来说这样的样式难免有些难看 这样的结果显然不能满足当下的需求,所以我决定利用官方给的 ... 
- BMap:WEB 服务API
		ylbtech-Map-Baidu: WEB 服务API 百度地图Web服务API为开发者提供http/https接口,即开发者通过http/https形式发起检索请求,获取返回json或xml格式的 ... 
随机推荐
- Hash索引与B-Tree索引
			Hash索引 Hash 索引结构的特殊性,其检索效率非常高,索引的检索可以一次定位,不像B-Tree 索引需要从根节点到枝节点,最后才能访问到页节点这样多次的IO访问,所以 Hash 索引的查询效率要 ... 
- java 切图 判断图片是否是纯色/彩色图片
			首先上切图的代码 /** * 图片剪裁 * @param x 距离左上角的x轴距离 * @param y 距离左上角的y轴距离 * @param width 宽度 * @param height 高度 ... 
- Linq-语句之存储过程
			存储过程 在我们编写程序中,往往需要一些存储过程,在LINQ to SQL中怎么使用呢?也许比原来的更简单些.下面我们以NORTHWND.MDF数据库中自带的几个存储过程来理解一下. 1.标量返回 在 ... 
- 在 Linux 中永久修改 USB 设备权限
			问题 当我尝试在 Linux 中运行 USB GPS 接收器时我遇到了下面来自 gpsd 的错误.看上去 gpsd 没有权限访问 USB 设备(/dev/ttyUSB0).我该如何永久修改它在Linu ... 
- Mysql创建多列唯一索引Sql
			ALTER TABLE `t_city_combo` ADD UNIQUE INDEX ` t_city_combo_index` (`combo_id`, `combo_name`, `city_i ... 
- 重命名IDEA14项目名
			Project Settings / Project ->">工程结构(ctrl-alt-shift-s)->设置->项目/项目Project name: 请注意,这 ... 
- 在Foreda8上试安装Apchehttpd-2.4.6.tar.gz
			下文是我边试边做的记录,不保证内容的完整性和正确性. 由于我的Apsire机器是最简安装Foreda8,所以需要安装httpd,熟悉一遍也是很好的嘛. 我从网上搜罗并下载了apchehttpd-2.4 ... 
- C#.NET常见问题(FAQ)-get set属性有什么意义
			使用get,set可以让类定义的更加规范,因为正常情况下,如果我们写一个自定义类,他的属性要么是public,要么是private,但是如果public的属性又要做限制,比如人年龄不允许负数,也不允许 ... 
- Office PPT如何切换到返回幻灯片
			1 如图所示,有"老师""同学""家人"三个板块,如果依次播放,将播放"老师1" "老师2" &qu ... 
- 优秀web资源
			http://www.filewatcher.com 一步一步asp.net_页面静态化管理 http://www.cnblogs.com/ylwn817/articles/2006923.html ... 
