BaiduApiService.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #region 描述
  2. //-----------------------------------------------------------------------------
  3. // 文 件 名: BaiduApiService
  4. // 作 者:ChinaYi
  5. // 创建时间:2014/6/10 11:09:07
  6. // 描 述:用于调用百度API
  7. // 版 本:4.0.30319.1022
  8. //-----------------------------------------------------------------------------
  9. // 历史更新纪录
  10. //-----------------------------------------------------------------------------
  11. // 版 本: 修改时间: 修改人:
  12. // 修改内容:
  13. //-----------------------------------------------------------------------------
  14. // Copyright (C) 20013-2014 泛美商务
  15. //-----------------------------------------------------------------------------
  16. #endregion
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Linq;
  20. using System.Text;
  21. using System.Net;
  22. using System.Xml;
  23. using Models;
  24. using System.Text.RegularExpressions;
  25. namespace DAL
  26. {
  27. public class BaiduApiService
  28. {
  29. public readonly string Key_Ak = "1Q1c6zkFGoapVIAF30a4OdKC"; //访问服务的key
  30. public string outPut = "xml"; //决定服务返回数据的类型,分别为xml和json
  31. public string url = ""; //服务路径
  32. /// <summary>
  33. ///天气查询
  34. /// </summary>
  35. public List<WeatherForecast> WeatherInquiry(string address)
  36. {
  37. url = string.Format("http://api.map.baidu.com/telematics/v3/weather?location={0}&output={1}&ak={2}",address,outPut,Key_Ak);
  38. WebClient client = new WebClient();
  39. client.Encoding = Encoding.UTF8; //编码格式
  40. string responseTest = client.DownloadString(url);
  41. XmlDocument doc = new XmlDocument();
  42. doc.LoadXml(responseTest);
  43. List<WeatherForecast> list = new List<WeatherForecast>();
  44. try
  45. {
  46. XmlNodeList nodeList = doc.GetElementsByTagName("results"); //第一层节点
  47. foreach (XmlNode item in nodeList)
  48. {
  49. //第二层的节点
  50. //wf.CurrentCity = item.SelectSingleNode("currentCity").InnerText;
  51. XmlNodeList date = doc.GetElementsByTagName("date");
  52. XmlNodeList weather = doc.GetElementsByTagName("weather");
  53. XmlNodeList wind = doc.GetElementsByTagName("wind");
  54. XmlNodeList temperature = doc.GetElementsByTagName("temperature");
  55. for (int i = 1; i < date.Count; i++)
  56. {
  57. WeatherForecast wf = new WeatherForecast();
  58. wf.CurrentCity = item.SelectSingleNode("currentCity").InnerText;
  59. wf.Date = date[i].InnerText;
  60. wf.Weather = weather[i - 1].InnerText;
  61. wf.Wind = wind[i - 1].InnerText;
  62. wf.Temperature = temperature[i - 1].InnerText;
  63. list.Add(wf);
  64. }
  65. //foreach (XmlNode nodeItem in nodeList)
  66. //{
  67. // //wf.Date = nodeItem.SelectSingleNode("date").InnerText;
  68. // //wf.DayPictureUrl = nodeItem.SelectSingleNode("dayPictureUrl").InnerText;
  69. // //wf.NightPictureUrl = nodeItem.SelectSingleNode("nightPictureUrl").InnerText;
  70. // //wf.Weather = nodeItem.SelectSingleNode("weather").InnerText;
  71. // //wf.Wind = nodeItem.SelectSingleNode("wind").InnerText;
  72. // //wf.Temperature = nodeItem.SelectSingleNode("temperature").InnerText;
  73. //}
  74. }
  75. }
  76. catch (Exception ex)
  77. {
  78. //禁止弹出黄页,跳向有友好提示的页面
  79. throw;
  80. }
  81. return list;
  82. }
  83. /// <summary>
  84. /// 获取当前所在的地理位置
  85. /// </summary>
  86. /// <param name="Jwd">经纬度</param>
  87. /// <returns></returns>
  88. //public string GetCityinfo(string jd,string wd)
  89. //{
  90. // WebClient client = new WebClient();//webclient客户端对象
  91. // url = string.Format("http://api.map.baidu.com/geocoder?location={0},{1}&output={2}&key={3}",jd,wd,outPut,Key_Ak);
  92. // client.Encoding = Encoding.UTF8; //编码格式
  93. // string responseTest = client.DownloadString(url); //下载xml响应数据
  94. // return responseTest;
  95. //}
  96. /// <summary>
  97. /// 周边检索
  98. /// </summary>
  99. /// <param name="address"></param>
  100. /// <param name="Tag"></param>
  101. /// <returns></returns>
  102. public List<VicinitySearch> VicinitySearch(string address,string Tag)
  103. {
  104. //根据城市获取经纬度
  105. url = string.Format("http://api.map.baidu.com/telematics/v3/geocoding?keyWord={0}&cityName={1}&out_coord_type=gcj02&ak={2}", address, address, Key_Ak);
  106. WebClient client = new WebClient();
  107. client.Encoding = Encoding.UTF8; //编码格式
  108. string jwd = client.DownloadString(url);//下载xml响应数据
  109. XmlDocument doc = new XmlDocument();
  110. doc.LoadXml(jwd);
  111. string lat = "";
  112. string lng = "";
  113. try
  114. {
  115. XmlNodeList nlist = doc.GetElementsByTagName("location");
  116. foreach (XmlNode item in nlist)
  117. {
  118. lat = item.SelectSingleNode("lat").InnerText;
  119. lng = item.SelectSingleNode("lng").InnerText;
  120. }
  121. }
  122. catch (Exception ex)
  123. {
  124. //该处禁止弹出黄页,请处理!
  125. throw;
  126. }
  127. //周边检索
  128. url = string.Format("http://api.map.baidu.com/telematics/v3/local?location={0},{1}&keyWord={2}&output=xml&ak={3}",lat,lng, Tag, Key_Ak);
  129. string responseTest = client.DownloadString(url);
  130. List<VicinitySearch> list = new List<Models.VicinitySearch>();
  131. XmlDocument xdoc = new XmlDocument();
  132. xdoc.LoadXml(responseTest);
  133. try
  134. {
  135. XmlNodeList nodeList = xdoc.GetElementsByTagName("point");
  136. foreach (XmlNode item in nodeList)
  137. {
  138. VicinitySearch vs = new VicinitySearch();
  139. vs.District = item.SelectSingleNode("district").InnerText;
  140. nodeList = xdoc.GetElementsByTagName("additionalInfo");
  141. foreach (XmlNode node in nodeList)
  142. {
  143. vs.Address = node.SelectSingleNode("address").InnerText;
  144. vs.Name = node.SelectSingleNode("name").InnerText;
  145. vs.Price = node.SelectSingleNode("price").InnerText;
  146. vs.Tag = node.SelectSingleNode("tag").InnerText;
  147. vs.Telephone = node.SelectSingleNode("telephone").InnerText;
  148. }
  149. list.Add(vs);
  150. }
  151. }
  152. catch (Exception ex)
  153. {
  154. //该处禁止弹出黄页,请处理
  155. throw;
  156. }
  157. return list;
  158. }
  159. /// <summary>
  160. /// 景点查询
  161. /// </summary>
  162. /// <param name="address"></param>
  163. /// <returns></returns>
  164. public Attractions AttractionsInquiry(string address)
  165. {
  166. url = string.Format("http://api.map.baidu.com/telematics/v3/travel_attractions?id={0}&ak={1}", address, Key_Ak);
  167. WebClient client = new WebClient();
  168. client.Encoding = Encoding.UTF8; //编码格式
  169. string responseTest = client.DownloadString(url);
  170. //解析xml文件并封装到实体中
  171. Attractions att = new Attractions();
  172. XmlDocument doc = new XmlDocument();
  173. //responseTest = responseTest.Replace("\n", "");
  174. responseTest = Regex.Replace(responseTest, "[\\x10]", " ");
  175. doc.LoadXml(responseTest);
  176. XmlNodeList nodeList = doc.GetElementsByTagName("result"); //第一层节点
  177. foreach (XmlNode item in nodeList)
  178. {
  179. att.Name = item.SelectSingleNode("name").InnerText;
  180. att.Summary = item.SelectSingleNode("abstract").InnerText;
  181. att.Description = item.SelectSingleNode("description").InnerText;
  182. }
  183. nodeList = doc.GetElementsByTagName("ticket_info"); //第二层节点
  184. foreach (XmlNode item in nodeList)
  185. {
  186. att.Price = item.SelectSingleNode("price").InnerText;
  187. att.Open_time = item.SelectSingleNode("open_time").InnerText;
  188. }
  189. nodeList = doc.GetElementsByTagName("item"); //第二层节点
  190. foreach (XmlNode item in nodeList)
  191. {
  192. att.TicketsIncentives = item.SelectSingleNode("description").InnerText;
  193. }
  194. return att;
  195. }
  196. }
  197. }