亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
1. WeChat’s geographical location information
2、地址位置的應(yīng)用處理
3、地址位置應(yīng)用擴(kuò)展
Home WeChat Applet WeChat Development C# develops WeChat portals and applications using geographical location to expand related applications

C# develops WeChat portals and applications using geographical location to expand related applications

Mar 01, 2017 am 10:33 AM

This article continues the previous article "C# Development of WeChat Portal and Applications (12)-Using Speech Processing" and continues to introduce WeChat related applications. We know that geographical location information can be used for many related applications. In addition to knowing the user's location, we can also associate some geographical location applications, such as weather, popular movies, nearby attractions, nearby theaters, traffic events, etc. Etc. Anyway, for all information related to geographical location, we can make some extended applications as needed. This article mainly introduces how to use geographical location information to build operations that use these applications.

C# develops WeChat portals and applications using geographical location to expand related applications

1. WeChat’s geographical location information

Before using it, let’s take a look at WeChat’s interface, which defines for us those information about geographical location. Information. In fact, the geographical location information is divided into two aspects on WeChat. One is to receive the user's geographical location request, and the other is the geographical location information that the user allows to report the geographical location operation and is sent regularly.

This article mainly introduces related applications based on the first type, how to process the user's geographical location after reporting it.

The geographical location reporting operation is to select the + sign to add the geographical location where you enter it, and then select the current or specified geographical location map. The specific operation is as follows.

C# develops WeChat portals and applications using geographical location to expand related applications ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? C# develops WeChat portals and applications using geographical location to expand related applications

Geolocation Message

<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1351776360</CreateTime>
<MsgType><![CDATA[location]]></MsgType>
<Location_X>23.134521</Location_X>
<Location_Y>113.358803</Location_Y>
<Scale>20</Scale>
<Label><![CDATA[位置信息]]></Label>
<MsgId>1234567890123456</MsgId>
</xml>


##FromUserNameSender account (an OpenID)CreateTimeMessage creation time (integer)MsgTypelocation##Location_XLocation_Y##ScaleMap zoom sizeLabelGeographical location informationMsgIdMessage id, 64-bit integer

有了上面的地理位置信息,我們?cè)诔绦蚶锩?,需要在消息傳遞過(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è)用戶選擇的指令提示,如下所示。

C# develops WeChat portals and applications using geographical location to expand related applications

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

C# develops WeChat portals and applications using geographical location to expand related applications

這樣整個(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)建一些模塊。

C# develops WeChat portals and applications using geographical location to expand related applications

C# develops WeChat portals and applications using geographical location to expand related applications

這上面都有很多相關(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# develops WeChat portals and applications using geographical location to expand related applications? ?C# develops WeChat portals and applications using geographical location to expand related applications

C# develops WeChat portals and applications using geographical location to expand related applications?C# develops WeChat portals and applications using geographical location to expand related applications

C# develops WeChat portals and applications using geographical location to expand related applications

For more C# development of WeChat portals and applications using geographical location to expand related applications, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

    <style id="fk3nv"></style>

      <cite id="fk3nv"></cite>
    <sub id="fk3nv"></sub>
      1. <style id="fk3nv"></style>
        ParametersDescription
        ToUserName DeveloperWeChat ID
        Geolocation dimension
        Geolocation longitude