#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 = ""; //服务路径 /// ///天气查询 /// public List 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 list = new List(); 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; } /// /// 获取当前所在的地理位置 /// /// 经纬度 /// //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; //} /// /// 周边检索 /// /// /// /// public List 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 list = new List(); 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; } /// /// 景点查询 /// /// /// 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; } } }