|
有了上面的地理位置信息,我們?cè)诔绦蚶锩妫枰谙鬟f過(guò)來(lái)的時(shí)候,定義一個(gè)實(shí)體類信息,承載相關(guān)的地理位置信息,方便我們進(jìn)一步的處理操作。
/// <summary>
/// 接收的地理位置消息
/// </summary>
[System.Xml.Serialization.XmlRoot(ElementName = "xml")]
public class RequestLocation : BaseMessage
{
public RequestLocation()
{
this.MsgType = RequestMsgType.Location.ToString().ToLower();
}
/// <summary>
/// 消息ID
/// </summary>
public Int64 MsgId { get; set; }
/// <summary>
/// 地理位置維度
/// </summary>
public decimal Location_X { get; set; }
/// <summary>
/// 地理位置經(jīng)度
/// </summary>
public decimal Location_Y { get; set; }
/// <summary>
/// 地圖縮放大小
/// </summary>
public int Scale { get; set; }
/// <summary>
/// 地理位置信息
/// </summary>
public string Label { get; set; }
}
2、地址位置的應(yīng)用處理
不過(guò)上面的信息,顯然不符合我們擴(kuò)展應(yīng)用的要求,因此我們進(jìn)一步進(jìn)行完善里面對(duì)地理位置信息處理的操作。我們進(jìn)一步把關(guān)于地理位置的操作,放到事件處理模塊里面進(jìn)行處理,處理代碼如下所示。
/// <summary>
/// 對(duì)地理位置請(qǐng)求信息進(jìn)行處理
/// </summary>
/// <param name="info">地理位置請(qǐng)求信息實(shí)體</param>
/// <returns></returns>
public string HandleLocation(Entity.RequestLocation info)
{
string xml = "";
ResponseText txtinfo = new ResponseText(info);
txtinfo.Content = string.Format("您發(fā)送的地理位置是:{0}", info.Label);
xml = txtinfo.ToXml();
return xml;
}
在處理的時(shí)候,我們需要先保存用戶的地理位置信息,把它存儲(chǔ)到用戶的上下文記錄里面。這樣我們?cè)谔幚碇噶畹臅r(shí)候,把它獲取到,然后傳遞給相關(guān)的方法就可以實(shí)現(xiàn)地理位置的擴(kuò)展應(yīng)用了。
//保存經(jīng)緯度
string location = string.Format("{0},{1}", lat, lon);
bool result = BLLFactory<UserSet>.Instance.UpdateUserInput(info.FromUserName, location);
首先對(duì)用戶地理位置的請(qǐng)求,我根據(jù)數(shù)據(jù)庫(kù)配置給出了一個(gè)用戶選擇的指令提示,如下所示。

為了對(duì)地理位置請(qǐng)求的處理,我定義了一個(gè)用于處理這個(gè)操作的指令操作

這樣整個(gè)地理位置的指令操作,就在應(yīng)答鏈里面進(jìn)行很好的跳轉(zhuǎn)管理了。那么為了實(shí)現(xiàn)天氣、放映影片、附近影院、旅游線路、交通事件等方面的擴(kuò)展應(yīng)用,我們應(yīng)該如何操作呢?
3、地址位置應(yīng)用擴(kuò)展
我們知道,百度或者騰訊都提供了一些開(kāi)放平臺(tái),給我們進(jìn)行各種方式的使用。那么我們這里以使用百度LBS平臺(tái)應(yīng)用來(lái)構(gòu)建一些模塊。

這上面都有很多相關(guān)的接口供使用,我們可以根據(jù)其提供的數(shù)據(jù)格式進(jìn)行封裝,然后進(jìn)行調(diào)用處理就可以了。
剛才說(shuō)了,我配置了一些指令,用來(lái)構(gòu)建相關(guān)的應(yīng)用,指令的最后是一些事件代碼的定義,我們對(duì)這些末端的事件代碼進(jìn)行處理,就可以給用戶返回相關(guān)的信息了,總體的操作代碼如下所示。
/// <summary>
/// 其他插件操作,如天氣,景點(diǎn)、電影影訊、交通等
/// </summary>
/// <param name="info">基礎(chǔ)消息</param>
/// <param name="eventKey">事件標(biāo)識(shí)</param>
/// <returns></returns>
public string DealPlugin(BaseMessage info, string eventKey)
{
//LogTextHelper.Info(eventKey);
string userInput = BLLFactory<UserSet>.Instance.GetUserInput(info.FromUserName);
string xml = "";
switch (eventKey)
{
case "event-void-wether":
xml = new WeatherPlugin().Response(info, userInput);
break;
case "event-void-movie":
xml = new MoviePlugin().Response(info, userInput);
break;
case "event-void-cinema":
xml = new CinemaPlugin().Response(info, userInput);
break;
case "event-void-travel":
xml = new TravelPlugin().Response(info, userInput);
break;
case "event-void-traffic":
xml = new TrafficEventPlugin().Response(info, userInput);
break;
default:
break;
}
return xml;
}
這里以天氣為例,說(shuō)明該如何調(diào)用百度的接口的,首先我們封裝一下相關(guān)的接口調(diào)用。
/// <summary>
/// 根據(jù)參數(shù)調(diào)用百度接口,獲取相關(guān)的結(jié)果數(shù)據(jù)
/// </summary>
/// <param name="location">地理位置</param>
/// <param name="ak">API調(diào)用鍵</param>
/// <returns></returns>
public BaiduWeatherResult Execute(string location, string ak)
{
location = HttpUtility.UrlEncode(location);
var url = string.Format("http://api.map.baidu.com/telematics/v3/weather?location={0}&output=json&ak={1}", location, ak);
BaiduWeatherResult result = BaiduJsonHelper<BaiduWeatherResult>.ConvertJson(url);
return result;
}
其中的BaiduWeatherResult 是我根據(jù)調(diào)用返回的Json結(jié)果,構(gòu)建的一個(gè)實(shí)體類,用來(lái)存儲(chǔ)返回的內(nèi)容。具體代碼如下所示。
/// <summary>
/// 天氣請(qǐng)求結(jié)果Json對(duì)象
/// </summary>
public class BaiduWeatherResult : BaiduResult
{
/// <summary>
/// 天氣預(yù)報(bào)信息
/// </summary>
public List<BaiduWeatherData> results = new List<BaiduWeatherData>();
}
/// <summary>
/// 城市的天氣信息
/// </summary>
public class BaiduWeatherData
{
/// <summary>
/// 當(dāng)前城市
/// </summary>
public string currentCity { get; set; }
/// <summary>
/// 天氣預(yù)報(bào)信息
/// </summary>
public List<BaiduWeatherJson> weather_data = new List<BaiduWeatherJson>();
}
/// <summary>
/// 天氣預(yù)報(bào)的單條記錄Json信息
/// </summary>
public class BaiduWeatherJson
{
/// <summary>
/// 天氣預(yù)報(bào)時(shí)間
/// </summary>
public string date { get; set; }
/// <summary>
/// 白天的天氣預(yù)報(bào)圖片url
/// </summary>
public string dayPictureUrl { get; set; }
/// <summary>
/// 晚上的天氣預(yù)報(bào)圖片url
/// </summary>
public string nightPictureUrl { get; set; }
/// <summary>
/// 天氣狀況
/// </summary>
public string weather { get; set; }
/// <summary>
/// 風(fēng)力
/// </summary>
public string wind { get; set; }
/// <summary>
/// 溫度
/// </summary>
public string temperature { get; set; }
}
為了構(gòu)建返回給客戶的圖文數(shù)據(jù),我們需要構(gòu)建一個(gè)News對(duì)象,然后生成XML數(shù)據(jù)返回給服務(wù)器進(jìn)行處理即可。
/// <summary>
/// 響應(yīng)用戶請(qǐng)求,并返回相應(yīng)的XML數(shù)據(jù)
/// </summary>
/// <param name="info">微信基礎(chǔ)信息</param>
/// <param name="location">地理位置:經(jīng)緯度坐標(biāo)或者地名</param>
/// <returns></returns>
public string Response(BaseMessage info, string location)
{
string xml = "";
//"廣州" 或者 "116.305145,39.982368"
if (!string.IsNullOrEmpty(location))
{
BaiduWeatherResult result = Execute(location, baiduAK);
if (result != null && result.results.Count > 0)
{
BaiduWeatherData data = result.results[0];
if (data != null)
{
ArticleEntity first = new ArticleEntity();
first.Title = string.Format("{0} 天氣預(yù)報(bào)", data.currentCity);
ResponseNews news = new ResponseNews(info);
news.Articles.Add(first);
int i = 0;
foreach (BaiduWeatherJson json in data.weather_data)
{
ArticleEntity article = new ArticleEntity();
article.Title = string.Format("{0}\n{1} {2} {3}", json.date, json.weather, json.wind, json.temperature);
if (i++ == 0)
{
article.PicUrl = IsDayTime() ? json.dayPictureUrl : json.nightPictureUrl;
}
else
{
article.PicUrl = json.dayPictureUrl;
}
news.Articles.Add(article);
}
xml = news.ToXml();
}
}
}
return xml;
}
這樣就很好實(shí)現(xiàn)了整體的功能了,具體界面功能可以訪問(wèn)我的微信(廣州愛(ài)奇迪)進(jìn)行了解,下面是功能截圖供參考。
? ?
?

?
更多C#開(kāi)發(fā)微信入口網(wǎng)站及應(yīng)用程式使用地理位置擴(kuò)充相關(guān)應(yīng)用相關(guān)文章請(qǐng)關(guān)注PHP中文網(wǎng)!
?