123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- #region 描述
- //-----------------------------------------------------------------------------
- // 文 件 名: BaiduApiService
- // 作 者:ChinaYi
- // 创建时间:2014/6/10 11:09:07
- // 描 述:用于调用百度API
- // 版 本:4.0.30319.1022
- //-----------------------------------------------------------------------------
- // 历史更新纪录
- //-----------------------------------------------------------------------------
- // 版 本: 修改时间: 修改人:
- // 修改内容:
- //-----------------------------------------------------------------------------
- // Copyright (C) 20013-2014 泛美商务
- //-----------------------------------------------------------------------------
- #endregion
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.Xml;
- using Models;
- using System.Text.RegularExpressions;
- namespace DAL
- {
- public class BaiduApiService
- {
- public readonly string Key_Ak = "1Q1c6zkFGoapVIAF30a4OdKC"; //访问服务的key
- public string outPut = "xml"; //决定服务返回数据的类型,分别为xml和json
- public string url = ""; //服务路径
- /// <summary>
- ///天气查询
- /// </summary>
- public List<WeatherForecast> WeatherInquiry(string address)
- {
- url = string.Format("http://api.map.baidu.com/telematics/v3/weather?location={0}&output={1}&ak={2}",address,outPut,Key_Ak);
- WebClient client = new WebClient();
- client.Encoding = Encoding.UTF8; //编码格式
- string responseTest = client.DownloadString(url);
- XmlDocument doc = new XmlDocument();
- doc.LoadXml(responseTest);
- List<WeatherForecast> list = new List<WeatherForecast>();
-
- try
- {
- XmlNodeList nodeList = doc.GetElementsByTagName("results"); //第一层节点
- foreach (XmlNode item in nodeList)
- {
- //第二层的节点
-
- //wf.CurrentCity = item.SelectSingleNode("currentCity").InnerText;
- XmlNodeList date = doc.GetElementsByTagName("date");
- XmlNodeList weather = doc.GetElementsByTagName("weather");
- XmlNodeList wind = doc.GetElementsByTagName("wind");
- XmlNodeList temperature = doc.GetElementsByTagName("temperature");
- for (int i = 1; i < date.Count; i++)
- {
- WeatherForecast wf = new WeatherForecast();
- wf.CurrentCity = item.SelectSingleNode("currentCity").InnerText;
- wf.Date = date[i].InnerText;
- wf.Weather = weather[i - 1].InnerText;
- wf.Wind = wind[i - 1].InnerText;
- wf.Temperature = temperature[i - 1].InnerText;
- list.Add(wf);
- }
- //foreach (XmlNode nodeItem in nodeList)
- //{
- // //wf.Date = nodeItem.SelectSingleNode("date").InnerText;
- // //wf.DayPictureUrl = nodeItem.SelectSingleNode("dayPictureUrl").InnerText;
- // //wf.NightPictureUrl = nodeItem.SelectSingleNode("nightPictureUrl").InnerText;
- // //wf.Weather = nodeItem.SelectSingleNode("weather").InnerText;
- // //wf.Wind = nodeItem.SelectSingleNode("wind").InnerText;
- // //wf.Temperature = nodeItem.SelectSingleNode("temperature").InnerText;
-
- //}
-
- }
-
- }
- catch (Exception ex)
- {
- //禁止弹出黄页,跳向有友好提示的页面
- throw;
- }
-
- return list;
- }
-
- /// <summary>
- /// 获取当前所在的地理位置
- /// </summary>
- /// <param name="Jwd">经纬度</param>
- /// <returns></returns>
- //public string GetCityinfo(string jd,string wd)
- //{
- // WebClient client = new WebClient();//webclient客户端对象
- // url = string.Format("http://api.map.baidu.com/geocoder?location={0},{1}&output={2}&key={3}",jd,wd,outPut,Key_Ak);
-
- // client.Encoding = Encoding.UTF8; //编码格式
- // string responseTest = client.DownloadString(url); //下载xml响应数据
- // return responseTest;
- //}
- /// <summary>
- /// 周边检索
- /// </summary>
- /// <param name="address"></param>
- /// <param name="Tag"></param>
- /// <returns></returns>
- public List<VicinitySearch> VicinitySearch(string address,string Tag)
- {
- //根据城市获取经纬度
- 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);
- WebClient client = new WebClient();
- client.Encoding = Encoding.UTF8; //编码格式
- string jwd = client.DownloadString(url);//下载xml响应数据
- XmlDocument doc = new XmlDocument();
- doc.LoadXml(jwd);
- string lat = "";
- string lng = "";
- try
- {
- XmlNodeList nlist = doc.GetElementsByTagName("location");
- foreach (XmlNode item in nlist)
- {
- lat = item.SelectSingleNode("lat").InnerText;
- lng = item.SelectSingleNode("lng").InnerText;
- }
- }
- catch (Exception ex)
- {
- //该处禁止弹出黄页,请处理!
- throw;
- }
-
- //周边检索
- 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);
- string responseTest = client.DownloadString(url);
- List<VicinitySearch> list = new List<Models.VicinitySearch>();
- XmlDocument xdoc = new XmlDocument();
- xdoc.LoadXml(responseTest);
- try
- {
- XmlNodeList nodeList = xdoc.GetElementsByTagName("point");
- foreach (XmlNode item in nodeList)
- {
- VicinitySearch vs = new VicinitySearch();
- vs.District = item.SelectSingleNode("district").InnerText;
- nodeList = xdoc.GetElementsByTagName("additionalInfo");
- foreach (XmlNode node in nodeList)
- {
- vs.Address = node.SelectSingleNode("address").InnerText;
- vs.Name = node.SelectSingleNode("name").InnerText;
- vs.Price = node.SelectSingleNode("price").InnerText;
- vs.Tag = node.SelectSingleNode("tag").InnerText;
- vs.Telephone = node.SelectSingleNode("telephone").InnerText;
- }
- list.Add(vs);
- }
- }
- catch (Exception ex)
- {
- //该处禁止弹出黄页,请处理
- throw;
- }
-
- return list;
- }
-
- /// <summary>
- /// 景点查询
- /// </summary>
- /// <param name="address"></param>
- /// <returns></returns>
- public Attractions AttractionsInquiry(string address)
- {
- url = string.Format("http://api.map.baidu.com/telematics/v3/travel_attractions?id={0}&ak={1}", address, Key_Ak);
- WebClient client = new WebClient();
- client.Encoding = Encoding.UTF8; //编码格式
- string responseTest = client.DownloadString(url);
- //解析xml文件并封装到实体中
- Attractions att = new Attractions();
- XmlDocument doc = new XmlDocument();
- //responseTest = responseTest.Replace("\n", "");
- responseTest = Regex.Replace(responseTest, "[\\x10]", " ");
- doc.LoadXml(responseTest);
- XmlNodeList nodeList = doc.GetElementsByTagName("result"); //第一层节点
- foreach (XmlNode item in nodeList)
- {
- att.Name = item.SelectSingleNode("name").InnerText;
- att.Summary = item.SelectSingleNode("abstract").InnerText;
- att.Description = item.SelectSingleNode("description").InnerText;
- }
- nodeList = doc.GetElementsByTagName("ticket_info"); //第二层节点
- foreach (XmlNode item in nodeList)
- {
- att.Price = item.SelectSingleNode("price").InnerText;
- att.Open_time = item.SelectSingleNode("open_time").InnerText;
- }
- nodeList = doc.GetElementsByTagName("item"); //第二层节点
- foreach (XmlNode item in nodeList)
- {
- att.TicketsIncentives = item.SelectSingleNode("description").InnerText;
- }
- return att;
- }
- }
- }
|