国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

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

來源:懂視網 責編:小采 時間:2020-11-27 22:36:50
文檔

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

微信公眾平臺開發之地理位置.Net代碼解析:微信公共平臺中涉及到地理位置的有兩種情況: 第一、我發送一個自選的地理位置給微信,然后微信可以自動反饋響應的信息。 第二、讓微信獲取我們GPS定位地址位置,反饋響應的信息。 首先我們先來看第一種,在微信中除了可以發文本,圖片,語音等還有一個信息就
推薦度:
導讀微信公眾平臺開發之地理位置.Net代碼解析:微信公共平臺中涉及到地理位置的有兩種情況: 第一、我發送一個自選的地理位置給微信,然后微信可以自動反饋響應的信息。 第二、讓微信獲取我們GPS定位地址位置,反饋響應的信息。 首先我們先來看第一種,在微信中除了可以發文本,圖片,語音等還有一個信息就

微信公共平臺中涉及到地理位置的有兩種情況:
        第一、我發送一個自選的地理位置給微信,然后微信可以自動反饋響應的信息。
        第二、讓微信獲取我們GPS定位地址位置,反饋響應的信息。 
       首先我們先來看第一種,在微信中除了可以發文本,圖片,語音等還有一個信息就是地理位置,按照微信接受地理信息的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代表經度,Scale代表縮放比例,Label代表位置的描述
 和接受文本,語音消息一下樣,地理信息的MsgType為“location”,修改一下之前的GetWxMessage()函數和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直接回復“你好”";
 res = sendTextMessage(wx, content);
 }
 else
 {
 content = "二維碼參數:\n" + wx.EventKey.Replace("qrscene_", "");
 res = sendTextMessage(wx, content);
 }
 }

 else if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.ToLower() == "scan")
 {
 string str = "二維碼參數:\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, "您發送的位置是:" + wx.Label + ";緯度是:" + wx.Location_X + ";經度是:" + wx.Location_Y + ";縮放比例為:" + wx.Scale);
 }
 else
 {
 res = sendTextMessage(wx, "你好,未能識別消息!");
 }
 }

 Response.Write(res);
 }

        這樣當我們發送一個地理位置信息的時候就可以反饋響應的信息了。值得一提的是:這里的地理信息位置無需授權,因為自己發送的地理信息位置不一定是自己的真實位置,我們可以在輸入界面進行任意選擇,不會涉及隱私。
        當然如果我們像制作類似于“我附近”的功能的時候,就必須有兩個條件,在微信公共號中開啟獲取用戶地理信息的功能。第二,用戶自己在關注微信的時候允許微信公共號獲取我的位置。這就需要用到我們在文章開始的時候給大家介紹的第二種情況了。按照微信的解釋,當一個會話開始的時候(也就是說進入對話界面的時候),首先獲取一下,然后每個五秒自動獲取一次。也就是就是說獲得用戶位置信息的時候觸發的不是“你一言我一語的對話”,而是一個特殊的事件,每格五秒出發一次。這里被定義為MsgType為“event”,而為了區別于其他的“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()函數和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;
 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的時候我們之前用到的是菜單的事件,現在我們需要加入其EventName為"LOCATION"的代碼段,因為現在還沒有涉及其他的event我后面就用else好了,后面我會把代碼寫的規范些。在這里分別給新增的三個屬性賦值,然后修改一下Onload函數 

 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直接回復“你好”";
 res = sendTextMessage(wx, content);
 }
 else
 {
 content = "二維碼參數:\n" + wx.EventKey.Replace("qrscene_", "");
 res = sendTextMessage(wx, content);
 }
 }

 else if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.ToLower() == "scan")
 {
 string str = "二維碼參數:\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, "您的位置是經度:" + wx.Latitude + ",維度是:" + wx.Longitude+",地理經度為:"+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, "您發送的位置是:" + wx.Label + ";緯度是:" + wx.Location_X + ";經度是:" + wx.Location_Y + ";縮放比例為:" + wx.Scale);
 }
 else
 {
 res = sendTextMessage(wx, "你好,未能識別消息!");
 }
 }

 Response.Write(res);
 }

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

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

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

微信公眾平臺開發之地理位置.Net代碼解析:微信公共平臺中涉及到地理位置的有兩種情況: 第一、我發送一個自選的地理位置給微信,然后微信可以自動反饋響應的信息。 第二、讓微信獲取我們GPS定位地址位置,反饋響應的信息。 首先我們先來看第一種,在微信中除了可以發文本,圖片,語音等還有一個信息就
推薦度:
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 草b视频在线观看 | 国产欧美日韩另类va在线 | 亚洲欧美自拍偷拍 | 理论片国产 | 日本久热| 国产特级黄色片 | 欧美日韩性视频在线 | 91发布页| 免费看一级黄色毛片 | 久久久精品一区二区三区 | 国产精品成人h片在线 | 日韩在线欧美在线 | 国内一级一级毛片a免费 | 国产69精品久久久久99不卡 | 日韩在线小视频 | 亚洲精品电影 | 国产高清在线精品一区二区三区 | 亚洲美女一区二区三区 | 日本黄一级日本黄二级 | 日韩欧美在线不卡 | 成人国内精品久久久久影院 | 可播放的免费男男videos不卡 | 免费一看一级毛片全播放 | 精品一区二区三区免费毛片爱 | 欧美日韩国产一区二区三区播放 | 欧美日韩一区二区三区四区 | 亚洲视频在线观看 | 精品日韩视频 | 午夜看一级特黄a大片黑 | 国产欧美日韩va | 国产一区二区成人 | 欧美精品一区二区在线观看 | 久久国产精品久久久久久久久久 | 中文国产成人精品久久96 | 国产欧美日韩一区二区三区 | 国内一区亚洲综合图区欧美 | 一区二区视频 | 国产激情视频一区二区三区 | 精品国产一区二区三区在线 | 在线国产观看 | 国产成人一区二区三区在线播放 |