濮阳杆衣贸易有限公司

主頁 > 知識庫 > 微信公眾平臺開發(fā)之地理位置.Net代碼解析

微信公眾平臺開發(fā)之地理位置.Net代碼解析

熱門標簽:旅游地圖標注線路 電話機器人鑰匙扣 威力最大的電銷機器人 電銷外呼系統(tǒng)是違法的嗎 廣西房產(chǎn)智能外呼系統(tǒng)推薦 地圖標注位置怎么弄圖 400電話唐山辦理 電銷專用外呼線路 漯河外呼調(diào)研線路

微信公共平臺中涉及到地理位置的有兩種情況:
        第一、我發(fā)送一個自選的地理位置給微信,然后微信可以自動反饋響應(yīng)的信息。
        第二、讓微信獲取我們GPS定位地址位置,反饋響應(yīng)的信息。 
       首先我們先來看第一種,在微信中除了可以發(fā)文本,圖片,語音等還有一個信息就是地理位置,按照微信接受地理信息的XML信息,我們需要改造一下之前的wxmessage類加上幾個屬性: 

class wxmessage 
  { 
    public string FromUserName { get; set; } 
    public string ToUserName { get; set; } 
    public string MsgType { get; set; } 
    public string EventName { get; set; } 
    public string Content { get; set; }
    public string Recognition { get; set; }
    public string MediaId { get; set; }
    public string EventKey { get; set; } 
    public string Location_X { get; set; }
    public string Location_Y { get; set; }
    public string Scale { get; set; }
    public string Label { get; set; }

  }    其中Location_X代表緯度,Location_Y代表經(jīng)度,Scale代表縮放比例,Label代表位置的描述
    和接受文本,語音消息一下樣,地理信息的MsgType為“l(fā)ocation”,修改一下之前的GetWxMessage()函數(shù)和OnLoad里面的消息處理:
 
private wxmessage GetWxMessage()
   {
     wxmessage wx = new wxmessage();
     StreamReader str = new StreamReader(Request.InputStream, System.Text.Encoding.UTF8);
     XmlDocument xml = new XmlDocument();
     xml.Load(str);
     wx.ToUserName = xml.SelectSingleNode("xml").SelectSingleNode("ToUserName").InnerText;
     wx.FromUserName = xml.SelectSingleNode("xml").SelectSingleNode("FromUserName").InnerText;
     wx.MsgType = xml.SelectSingleNode("xml").SelectSingleNode("MsgType").InnerText;
     if (wx.MsgType.Trim() == "text")
     {
       wx.Content = xml.SelectSingleNode("xml").SelectSingleNode("Content").InnerText;
     }
     if (wx.MsgType.Trim() == "location")
     {
       wx.Location_X = xml.SelectSingleNode("xml").SelectSingleNode("Location_X").InnerText;
       wx.Location_Y = xml.SelectSingleNode("xml").SelectSingleNode("Location_Y").InnerText;
       wx.Scale = xml.SelectSingleNode("xml").SelectSingleNode("Scale").InnerText;
       wx.Label = xml.SelectSingleNode("xml").SelectSingleNode("Label").InnerText;

     }
     if (wx.MsgType.Trim() == "event")
     {
       wx.EventName = xml.SelectSingleNode("xml").SelectSingleNode("Event").InnerText;
       wx.EventKey = xml.SelectSingleNode("xml").SelectSingleNode("EventKey").InnerText;
     }
     if (wx.MsgType.Trim() == "voice")
     {
       wx.Recognition = xml.SelectSingleNode("xml").SelectSingleNode("Recognition").InnerText;
     }
     
     return wx;
   }
  protected void Page_Load(object sender, EventArgs e)
   {
     wxmessage wx = GetWxMessage();
     string res = "";


     if (!string.IsNullOrEmpty(wx.EventName)  wx.EventName.Trim() == "subscribe")
     {
       string content = "";
       if (!wx.EventKey.Contains("qrscene_"))
       {
         content = "/:rose歡迎北京永杰友信科技有限公司/:rose\n直接回復(fù)“你好”";
         res = sendTextMessage(wx, content);
       }
       else
       {
         content = "二維碼參數(shù):\n" + wx.EventKey.Replace("qrscene_", "");
         res = sendTextMessage(wx, content);
       }
     }

     else if (!string.IsNullOrEmpty(wx.EventName)  wx.EventName.ToLower() == "scan")
     {
       string str = "二維碼參數(shù):\n" + wx.EventKey;
       res = sendTextMessage(wx, str);
     }
     else if (!string.IsNullOrEmpty(wx.EventName)  wx.EventName.Trim() == "CLICK")
     {
       if(wx.EventKey=="HELLO")
         res = sendTextMessage(wx, "你好,歡迎使用北京永杰友信科技有限公司公共微信平臺!");
     }
     else
     {
       WriteLog(wx.MsgType);
       if (wx.MsgType == "text"  wx.Content == "你好")
       {
         res = sendTextMessage(wx, "你好,歡迎使用北京永杰友信科技有限公司公共微信平臺!");
       }
       else if (wx.MsgType == "voice")
       {
         res = sendTextMessage(wx, wx.Recognition);
       }
       else if (wx.MsgType == "location")
       {
         res = sendTextMessage(wx, "您發(fā)送的位置是:" + wx.Label + ";緯度是:" + wx.Location_X + ";經(jīng)度是:" + wx.Location_Y + ";縮放比例為:" + wx.Scale);
       }
       else
       {
         res = sendTextMessage(wx, "你好,未能識別消息!");
       }
     }

     Response.Write(res);
   }

        這樣當我們發(fā)送一個地理位置信息的時候就可以反饋響應(yīng)的信息了。值得一提的是:這里的地理信息位置無需授權(quán),因為自己發(fā)送的地理信息位置不一定是自己的真實位置,我們可以在輸入界面進行任意選擇,不會涉及隱私。
        當然如果我們像制作類似于“我附近”的功能的時候,就必須有兩個條件,在微信公共號中開啟獲取用戶地理信息的功能。第二,用戶自己在關(guān)注微信的時候允許微信公共號獲取我的位置。這就需要用到我們在文章開始的時候給大家介紹的第二種情況了。按照微信的解釋,當一個會話開始的時候(也就是說進入對話界面的時候),首先獲取一下,然后每個五秒自動獲取一次。也就是就是說獲得用戶位置信息的時候觸發(fā)的不是“你一言我一語的對話”,而是一個特殊的事件,每格五秒出發(fā)一次。這里被定義為MsgType為“event”,而為了區(qū)別于其他的“event”,他的EventName(其實官方叫做event)為“LOCATION”(大寫哦)。
        下面我依然需要按照微信的格式修改我們的wxmessage類: 

 class wxmessage 
  { 
    public string FromUserName { get; set; } 
    public string ToUserName { get; set; } 
    public string MsgType { get; set; } 
    public string EventName { get; set; } 
    public string Content { get; set; }
    public string Recognition { get; set; }
    public string MediaId { get; set; }
    public string EventKey { get; set; } 
    public string Location_X { get; set; }
    public string Location_Y { get; set; }
    public string Scale { get; set; }
    public string Label { get; set; }
    public string Latitude { get; set; }
    public string Longitude { get; set; }
    public string Precision { get; set; }

  }
    改造一下GetWxMessage()函數(shù)和OnLoad函數(shù):
 
private wxmessage GetWxMessage()
   {
     wxmessage wx = new wxmessage();
     StreamReader str = new StreamReader(Request.InputStream, System.Text.Encoding.UTF8);
     XmlDocument xml = new XmlDocument();
     xml.Load(str);
     wx.ToUserName = xml.SelectSingleNode("xml").SelectSingleNode("ToUserName").InnerText;
     wx.FromUserName = xml.SelectSingleNode("xml").SelectSingleNode("FromUserName").InnerText;
     wx.MsgType = xml.SelectSingleNode("xml").SelectSingleNode("MsgType").InnerText;
     WriteLog("MsgType:"+wx.MsgType);
     if (wx.MsgType.Trim() == "event")
     {
       wx.EventName = xml.SelectSingleNode("xml").SelectSingleNode("Event").InnerText;
       WriteLog(wx.EventName);
       if (wx.EventName.ToUpper() == "LOCATION")
       {
         wx.Latitude = xml.SelectSingleNode("xml").SelectSingleNode("Latitude").InnerText;
         wx.Longitude = xml.SelectSingleNode("xml").SelectSingleNode("Longitude").InnerText;
         wx.Precision = xml.SelectSingleNode("xml").SelectSingleNode("Precision").InnerText;
       }
       else
       {
         wx.EventKey = xml.SelectSingleNode("xml").SelectSingleNode("EventKey").InnerText;
       }
     }
     if (wx.MsgType.Trim() == "text")
     {
       wx.Content = xml.SelectSingleNode("xml").SelectSingleNode("Content").InnerText;
     }
     if (wx.MsgType.Trim() == "location")
     {
       wx.Location_X = xml.SelectSingleNode("xml").SelectSingleNode("Location_X").InnerText;
       wx.Location_Y = xml.SelectSingleNode("xml").SelectSingleNode("Location_Y").InnerText;
       wx.Scale = xml.SelectSingleNode("xml").SelectSingleNode("Scale").InnerText;
       wx.Label = xml.SelectSingleNode("xml").SelectSingleNode("Label").InnerText;

     }
     
     if (wx.MsgType.Trim() == "voice")
     {
       wx.Recognition = xml.SelectSingleNode("xml").SelectSingleNode("Recognition").InnerText;
     }
     
     return wx;
   }

       當MsgType為event的時候我們之前用到的是菜單的事件,現(xiàn)在我們需要加入其EventName為"LOCATION"的代碼段,因為現(xiàn)在還沒有涉及其他的event我后面就用else好了,后面我會把代碼寫的規(guī)范些。在這里分別給新增的三個屬性賦值,然后修改一下Onload函數(shù) 

 protected void Page_Load(object sender, EventArgs e)
   {

     wxmessage wx = GetWxMessage();
     string res = "";


     if (!string.IsNullOrEmpty(wx.EventName)  wx.EventName.Trim() == "subscribe")
     {
       string content = "";
       if (!wx.EventKey.Contains("qrscene_"))
       {
         content = "/:rose歡迎北京永杰友信科技有限公司/:rose\n直接回復(fù)“你好”";
         res = sendTextMessage(wx, content);
       }
       else
       {
         content = "二維碼參數(shù):\n" + wx.EventKey.Replace("qrscene_", "");
         res = sendTextMessage(wx, content);
       }
     }

     else if (!string.IsNullOrEmpty(wx.EventName)  wx.EventName.ToLower() == "scan")
     {
       string str = "二維碼參數(shù):\n" + wx.EventKey;
       res = sendTextMessage(wx, str);
     }
     else if (!string.IsNullOrEmpty(wx.EventName)  wx.EventName.Trim() == "CLICK")
     {
       if(wx.EventKey=="HELLO")
         res = sendTextMessage(wx, "你好,歡迎使用北京永杰友信科技有限公司公共微信平臺!");
     }
     else if (!string.IsNullOrEmpty(wx.EventName)  wx.EventName.Trim() == "LOCATION")
     {
       res = sendTextMessage(wx, "您的位置是經(jīng)度:" + wx.Latitude + ",維度是:" + wx.Longitude+",地理經(jīng)度為:"+wx.Precision);
     }
     else
     {
       if (wx.MsgType == "text"  wx.Content == "你好")
       {
         res = sendTextMessage(wx, "你好,歡迎使用北京永杰友信科技有限公司公共微信平臺!");
       }
       else if (wx.MsgType == "voice")
       {
         res = sendTextMessage(wx, wx.Recognition);
       }
       else if (wx.MsgType == "location")
       {
         res = sendTextMessage(wx, "您發(fā)送的位置是:" + wx.Label + ";緯度是:" + wx.Location_X + ";經(jīng)度是:" + wx.Location_Y + ";縮放比例為:" + wx.Scale);
       }
       else
       {
         res = sendTextMessage(wx, "你好,未能識別消息!");
       }
     }

     Response.Write(res);
   }

        好了,完成,這樣當你開啟你的微信“獲得用戶位置信息”的時候微信平臺會提醒你,是僅進入會話第一次獲取,還是每個5秒獲取一次,如果你選擇了后者,你就會看到,每5秒會反饋給你一個地理位置的信息。
        這里面需要非常注意的是:我按照這樣認為沒有問題了,但是怎么也獲得不了信息,那是因為我在進入會話的時候,你會看到你的手機GPS在搜索,在GPS定位以前,是不會看到內(nèi)容的。可以這樣理解,當你GPS搜索定位后,才會觸發(fā)獲得用戶位置信息的事件,這一點并不是我想象的通過基站定位也可以獲得大致的位置,這一點需要開發(fā)者注意,我就是弄了半天,等我出門兒,手機定位了無意間看到了回復(fù),這才恍然大悟。
        說到這里可以各位會問只知道經(jīng)緯度坐標有什么用?又不是具體位置。其實不然,我們可以使用多種方法知道位置詳細的信息,例如我們可以通過BaiduMap API的地址反向解析指導這個坐標在那個城市,那個街道等內(nèi)容,甚至可以知道附近的情況,這里就不再多說了,以后有機會和大家一起來談?wù)凚aiduMap

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。 

您可能感興趣的文章:
  • 微信公眾平臺開發(fā)之發(fā)送圖文消息.Net代碼解析
  • 微信公眾平臺開發(fā)之自定義菜單.Net代碼解析
  • .Net微信開發(fā)之如何解決access_token過期問題
  • 微信公眾平臺開發(fā)之發(fā)送文本消息.Net代碼解析
  • .net實現(xiàn)微信公眾賬號接口開發(fā)實例代碼
  • .net開發(fā)微信公眾平臺實例教程
  • asp.net微信開發(fā)(永久素材管理)
  • asp.net開發(fā)微信公眾平臺之獲取用戶消息并處理
  • asp.net微信開發(fā)(開發(fā)者接入)
  • .NET微信公眾號 用戶分組管理

標簽:欽州 銅陵 焦作 湘西 湖北 試駕邀約 綏化 無錫

巨人網(wǎng)絡(luò)通訊聲明:本文標題《微信公眾平臺開發(fā)之地理位置.Net代碼解析》,本文關(guān)鍵詞  微信,公眾,平臺,開,發(fā)之,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《微信公眾平臺開發(fā)之地理位置.Net代碼解析》相關(guān)的同類信息!
  • 本頁收集關(guān)于微信公眾平臺開發(fā)之地理位置.Net代碼解析的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    郴州市| 洛南县| 永平县| 海城市| 安丘市| 平原县| 青神县| 韶山市| 浦东新区| 岢岚县| 石阡县| 丰镇市| 潮安县| 香河县| 常山县| 塘沽区| 海口市| 江阴市| 确山县| 遂宁市| 剑阁县| 祥云县| 城固县| 襄汾县| 大庆市| 华亭县| 大名县| 安丘市| 文成县| 漳平市| 赣州市| 吕梁市| 白河县| 衡山县| 满洲里市| 淳化县| 平远县| 清徐县| 眉山市| 奉节县| 南华县|