1、创建基础参数类

  1. public static class BaiduConstParams
  2. {
  3. public const string PlaceApIv2Search = "http://api.map.baidu.com/place/v2/search";
  4. public const string PlaceApIv2Detail = "http://api.map.baidu.com/place/v2/detail";
  5. public const string PlaceApIv2Eventsearch = "http://api.map.baidu.com/place/v2/eventsearch";
  6. public const string PlaceApIv2Eventdetail = "http://api.map.baidu.com/place/v2/eventdetail";
  7. public const string GeocodingApIv2 = "http://api.map.baidu.com/geocoder/v2/";
  8. public const string GeocodingApIv2Reverse = "http://api.map.baidu.com/geocoder/v2/";
  9. public const string TranslateApi = "http://openapi.baidu.com/public/2.0/bmt/translate";
  10. public const string GeoconvApi = "http://api.map.baidu.com/geoconv/v1/";
  11. }
  12.  
  13. public static class BaiduErrorMessages
  14. {
  15. public const string NotKey = "密钥不存在";
  16. public const string LackParam = "缺少必要请求参数";
  17. }

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

  1. public enum BaiduLbsType
  2. {
  3. PlaceApIv2Search,
  4. PlaceApIv2Detail,
  5. PlaceApIv2Eventsearch,
  6. PlaceApIv2Eventdetail,
  7. GeocodingApIv2,
  8. GeocodingApIv2Reverse,
  9. Translate,
  10. Geoconv
  11. }
  12.  
  13. public enum Status
  14. {
  15. /// <summary>
  16. /// 正常
  17. /// </summary>
  18. Ok = ,
  19. /// <summary>
  20. /// 请求参数非法
  21. /// </summary>
  22. ParameterInvalid = ,
  23. /// <summary>
  24. /// 权限校验失败
  25. /// </summary>
  26. VerifyFailure = ,
  27. /// <summary>
  28. /// 配额校验失败
  29. /// </summary>
  30. QuotaFailure = ,
  31. /// <summary>
  32. /// 不存在或者非法
  33. /// </summary>
  34. AkFailure = ,
  35. /// <summary>
  36. /// Transform 内部错误
  37. /// </summary>
  38. InternalError = ,
  39. /// <summary>
  40. /// from非法
  41. /// </summary>
  42. FromIllegal = ,
  43. /// <summary>
  44. /// to非法
  45. /// </summary>
  46. ToIllegal = ,
  47. /// <summary>
  48. /// coords非法
  49. /// </summary>
  50. CoordsIllegal = ,
  51. /// <summary>
  52. /// coords个数非法,超过限制
  53. /// </summary>
  54. CoordsCountIllegal =
  55.  
  56. }

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

  1. public class BaiduGeocodingResults
  2. {
  3. /// <summary>
  4. /// 返回结果状态值, 成功返回0,其他值请查看附录。
  5. /// </summary>
  6. [JsonProperty(PropertyName = "status")]
  7. public Status Status;
  8.  
  9. /// <summary>
  10. /// 返回结果状态值, 成功返回0,其他值请查看附录。
  11. /// </summary>
  12. [JsonProperty(PropertyName = "result")]
  13. public BaiduGeocodingResult Result;
  14. }
  15.  
  16. public class BaiduGeocodingResult
  17. {
  18. /// <summary>
  19. /// 经纬度坐标
  20. /// </summary>
  21. [JsonProperty(PropertyName = "location")]
  22. public BaiduGeocodingLoaction Location;
  23. /// <summary>
  24. /// 位置的附加信息,是否精确查找。1为精确查找,0为不精确。
  25. /// </summary>
  26. [JsonProperty(PropertyName = "precise")]
  27. public int Precise;
  28. /// <summary>
  29. /// 可信度
  30. /// </summary>
  31. [JsonProperty(PropertyName = "confidence")]
  32. public int Confidence;
  33. /// <summary>
  34. /// 地址类型
  35. /// </summary>
  36. [JsonProperty(PropertyName = "level")]
  37. public string Level;
  38.  
  39. /// <summary>
  40. /// 结构化地址信息
  41. /// </summary>
  42. [JsonProperty(PropertyName = "formatted_address")]
  43. public string FormattedAddress;
  44.  
  45. /// <summary>
  46. /// 所在商圈信息,如 "人民大学,中关村,苏州街"
  47. /// </summary>
  48. [JsonProperty(PropertyName = "business")]
  49. public string Business;
  50.  
  51. /// <summary>
  52. /// 具体地址
  53. /// </summary>
  54. [JsonProperty(PropertyName = "addressComponent")]
  55. public BaiduGeocodingAddress AddressComponent;
  56. }
  57.  
  58. public class BaiduGeocodingLoaction
  59. {
  60. /// <summary>
  61. /// 纬度值
  62. /// </summary>
  63. [JsonProperty(PropertyName = "lat")]
  64. public decimal Lat;
  65. /// <summary>
  66. /// 经度值
  67. /// </summary>
  68. [JsonProperty(PropertyName = "lng")]
  69. public decimal Lng;
  70. }
  71.  
  72. public class BaiduGeocodingAddress
  73. {
  74. /// <summary>
  75. /// 城市名
  76. /// </summary>
  77. [JsonProperty(PropertyName = "city")]
  78. public string City;
  79. /// <summary>
  80. /// 区县名
  81. /// </summary>
  82. [JsonProperty(PropertyName = "district")]
  83. public string District;
  84. /// <summary>
  85. /// 省名
  86. /// </summary>
  87. [JsonProperty(PropertyName = "province")]
  88. public string Province;
  89. /// <summary>
  90. /// 街道名
  91. /// </summary>
  92. [JsonProperty(PropertyName = "street")]
  93. public string Street;
  94. /// <summary>
  95. /// 街道门牌号
  96. /// </summary>
  97. [JsonProperty(PropertyName = "street_number")]
  98. public string StreetNumber;
  99. }

4、创建API通用处理类

  1. public class BaiduLbs
  2. {
  3. private readonly string _key;
  4.  
  5. public static string CurrentRequest = "";
  6.  
  7. public BaiduLbs(string key)
  8. {
  9. _key = key;
  10. }
  11.  
  12. /// <summary>
  13. /// 请求
  14. /// </summary>
  15. /// <param name="param"></param>
  16. /// <param name="baiduLbsType"></param>
  17. /// <param name="encoding"></param>
  18. /// <param name="action"></param>
  19. public void Request(string param, BaiduLbsType baiduLbsType, Encoding encoding, Action<string> action)
  20. {
  21. WebClient webClient = new WebClient { Encoding = encoding };
  22. string url = "";
  23. switch (baiduLbsType)
  24. {
  25. case BaiduLbsType.PlaceApIv2Search:
  26. url = string.Format(BaiduConstParams.PlaceApIv2Search + "?{0}", param);
  27. break;
  28. case BaiduLbsType.PlaceApIv2Detail:
  29. url = string.Format(BaiduConstParams.PlaceApIv2Detail + "?{0}", param);
  30. break;
  31. case BaiduLbsType.PlaceApIv2Eventsearch:
  32. url = string.Format(BaiduConstParams.PlaceApIv2Eventsearch + "?{0}", param);
  33. break;
  34. case BaiduLbsType.PlaceApIv2Eventdetail:
  35. url = string.Format(BaiduConstParams.PlaceApIv2Eventdetail + "?{0}", param);
  36. break;
  37. case BaiduLbsType.GeocodingApIv2:
  38. case BaiduLbsType.GeocodingApIv2Reverse:
  39. url = string.Format(BaiduConstParams.GeocodingApIv2 + "?{0}", param);
  40. break;
  41. case BaiduLbsType.Translate:
  42. url = string.Format(BaiduConstParams.TranslateApi + "?{0}", param);
  43. break;
  44. case BaiduLbsType.Geoconv:
  45. url = string.Format(BaiduConstParams.GeoconvApi + "?{0}", param);
  46. break;
  47.  
  48. }
  49. CurrentRequest = url;
  50. action(webClient.DownloadString(url));
  51. }
  52.  
  53. /// <summary>
  54. /// 响应
  55. /// </summary>
  56. /// <typeparam name="T"></typeparam>
  57. /// <param name="param"></param>
  58. /// <param name="baiduLbsType"></param>
  59. /// <param name="encoding"></param>
  60. public T Response<T>(string param, BaiduLbsType baiduLbsType, Encoding encoding)
  61. {
  62. T t = default(T);
  63.  
  64. Request(param, baiduLbsType, encoding, json =>
  65. {
  66. if (baiduLbsType == BaiduLbsType.GeocodingApIv2 || baiduLbsType == BaiduLbsType.GeocodingApIv2Reverse)
  67. {
  68. if (json.Contains("\"result\":[]"))
  69. {
  70. json = json.Replace("\"result\":[]", "\"result\":{}");
  71. }
  72. }
  73. t = (T)JsonConvert.DeserializeObject(json, typeof(T));
  74. });
  75. return t;
  76. }
  77.  
  78. public BaiduGeocodingResults BaiduGeocoding(string address, string city)
  79. {
  80. address = System.Web.HttpUtility.UrlEncode(address);
  81. city = System.Web.HttpUtility.UrlEncode(city);
  82. string request = string.Format("address={0}&output=json&ak={1}&city={2}", address, _key, city);
  83. var result = Response<BaiduGeocodingResults>(request, BaiduLbsType.GeocodingApIv2, Encoding.UTF8);
  84. if (result.Status == Status.Ok && result.Result.Location == null)
  85. {
  86. request = string.Format("address={0}&output=json&ak={1}&city={2}", city + address, _key, city);
  87. return Response<BaiduGeocodingResults>(request, BaiduLbsType.GeocodingApIv2, Encoding.UTF8);
  88. }
  89. return result;
  90. }
  91.  
  92. public BaiduGeocodingResults BaiduGeocoding(string longitude, string dimensions, string pois)
  93. {
  94. var location = longitude + "," + dimensions;
  95. string request = string.Format("ak={0}&location={1}&pois={2}", _key, location, pois);
  96. return Response<BaiduGeocodingResults>(request, BaiduLbsType.GeocodingApIv2, Encoding.UTF8);
  97. }
  98.  
  99. public GeoconvResults BaiduGeoconv(GeoconvParams geoconvParams, ref List<GeoconvPOI> geoconvPois)
  100. {
  101. geoconvParams.Ak = _key;
  102. return Response<GeoconvResults>(geoconvParams.ToString(ref geoconvPois), BaiduLbsType.Geoconv, Encoding.UTF8);
  103. }
  104.  
  105. public GeoconvResults BaiduGeoconv(GeoconvParams geoconvParams, GeoconvPOI geoconvPoi)
  106. {
  107. geoconvParams.Ak = _key;
  108. List<GeoconvPOI> geoconvPois = new List<GeoconvPOI>
  109. {
  110. geoconvPoi
  111. };
  112. return Response<GeoconvResults>(geoconvParams.ToString(ref geoconvPois), BaiduLbsType.Geoconv, Encoding.UTF8);
  113. }
  114. }

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. [leetcode]Populating Next Right Pointers in Each Node @ Python

    原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/ 题意: 1 / \ 2 3 / \ ...

  2. iOS开发-Quartz2D初识

    Quartz2D如果单独的从Quartz,那么会发现Quartz是一个开源的Java作业调度框架,单独从英文翻译的角度来看的话Quartz的英文是石英,如果有的时候不小心搜索会发现手表推荐.本文中介绍 ...

  3. Loadrunner视频教程汇总

    小布老师视频:测试工具概述,兼LoadRunner介绍 -1-4http://www.boobooke.com/v/bbk1046http://www.boobooke.com/v/bbk1046.z ...

  4. Dropwizard框架入门

    最近项目用到了Dropwizard框架,个人感觉还不错,那么这里就从他们官网入手,然后加上自己的实现步骤让大家初步了解这个框架. 官网对DW(Dropwizard)的定义是跨越了一个库和框架之间的界限 ...

  5. 检测 USB 设备拨插的 C# 类库:USBClassLibrary

    这是采用C#开发的一个USB库,使您可以管理USB设备的连接和分离事件,探测自己的设备.可以运行在Windows XP和Windows7 64位系统下. 01 private void USBPort ...

  6. 说一说activity

    activity与service,provider,receiver并称为 android的四大对象. 而activity,是展现界面的必不可少的组件.我这里有几个问题要问了,他是如何加载,他是如何进 ...

  7. 反汇编基本原理与x86指令构造

    反汇编基本原理与x86指令构造 概要:旨在讲述程序的二进制代码转换到汇编.即反汇编的基本原理.以及 x86 架构的 CPU 的指令构造,有这个基础后就能够自己编写汇编程序了,也能够将二进制代码数据转换 ...

  8. 【nodejs】用express又做了份crud

    感觉crud是高级形式的hello world了. app代码: 'use strict'; var express=require('express'); var http=require('htt ...

  9. iOS app集成支付宝支付流程及后台php订单签名处理

    iOS app集成支付宝支付流程 1: 开通支付宝商户 由公司去支付宝 https://b.alipay.com/order/serviceIndex.htm 签约支付宝开通支付宝商家: 2:商户支付 ...

  10. Android网络:开发浏览器(二)——功能完善之书签功能

    经过上述的编写,基本的功能已经完成了,不过工具栏里面基本还是一片空白,只有一个刷新的功能,现在咱们就先完善这些功能(之前有朋友说来点图,那么这次我会截些图更好的来描述). 既然是浏览器,怎么能没有书签 ...