首頁 微信小程式 微信開發 .NET開發微信公眾平台之地理位置實例詳解

.NET開發微信公眾平台之地理位置實例詳解

Apr 22, 2017 pm 02:59 PM
.net 地理位置 微信

這篇文章主要為大家詳細解析了微信公眾平台開發之地理位置.Net程式碼,有興趣的小夥伴們可以參考一下

微信公共平台中涉及到地理位置的有兩種狀況:
        第一、我傳送一個自選的地理位置給微信,然後微信可以自動回饋回應的訊息。
        第二、讓微信取得我們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的時候我們之前用到的是菜單的事件,現在我們需要加入其EventEventName"的程式碼段,因為現在還沒有涉及其他的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

以上是.NET開發微信公眾平台之地理位置實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1665
14
CakePHP 教程
1424
52
Laravel 教程
1321
25
PHP教程
1269
29
C# 教程
1249
24
H5頁面製作是前端開發嗎 H5頁面製作是前端開發嗎 Apr 05, 2025 pm 11:42 PM

是的,H5頁面製作是前端開發的重要實現方式,涉及HTML、CSS和JavaScript等核心技術。開發者通過巧妙結合這些技術,例如使用&lt;canvas&gt;標籤繪製圖形或使用JavaScript控制交互行為,構建出動態且功能強大的H5頁面。

公司安全軟件導致應用無法運行?如何排查和解決? 公司安全軟件導致應用無法運行?如何排查和解決? Apr 19, 2025 pm 04:51 PM

公司安全軟件導致部分應用無法正常運行的排查與解決方法許多公司為了保障內部網絡安全,會部署安全軟件。 ...

企業微信中的JS資源緩存問題如何解決? 企業微信中的JS資源緩存問題如何解決? Apr 04, 2025 pm 05:06 PM

企業微信的JS資源緩存問題探討在進行項目功能升級時,常常會遇到部分用戶未能成功升級的情況,尤其是在企�...

H5頁面製作和微信小程序有什麼不同 H5頁面製作和微信小程序有什麼不同 Apr 05, 2025 pm 11:51 PM

H5更靈活,可定制性強,但需要嫻熟的技術;小程序上手快,維護便捷,但受限於微信框架。

H5和小程序與APP的區別 H5和小程序與APP的區別 Apr 06, 2025 am 10:42 AM

H5、小程序和APP的主要區別在於:技術架構:H5基於網頁技術,小程序和APP為獨立應用程序。體驗和功能:H5輕便易用,功能受限;小程序輕量級,交互性好;APP功能強大,體驗流暢。兼容性:H5跨平台兼容,小程序和APP受平台限制。開發成本:H5開發成本低,小程序中等,APP最高。適用場景:H5適合信息展示,小程序適合輕量化應用,APP適合複雜功能應用。

mysql 需要互聯網嗎 mysql 需要互聯網嗎 Apr 08, 2025 pm 02:18 PM

MySQL 可在無需網絡連接的情況下運行,進行基本的數據存儲和管理。但是,對於與其他系統交互、遠程訪問或使用高級功能(如復制和集群)的情況,則需要網絡連接。此外,安全措施(如防火牆)、性能優化(選擇合適的網絡連接)和數據備份對於連接到互聯網的 MySQL 數據庫至關重要。

H5頁面製作的學習資源 H5頁面製作的學習資源 Apr 06, 2025 am 07:51 AM

學習H5頁面製作需掌握HTML、CSS、JavaScript三劍客,深入研究HTML5新特性、CSS選擇器、佈局、動畫等知識,掌握JavaScript基礎、庫,進階技巧包括動畫效果、響應式設計、服務器交互。遇到問題可利用搜索引擎、技術社區、開發者諮詢解決。持續學習新技術保持競爭力,選擇適合的學習資源,堅持練習是關鍵。

H5和小程序的開發工具有哪些 H5和小程序的開發工具有哪些 Apr 06, 2025 am 09:54 AM

H5開發工具推薦:VSCode、WebStorm、Atom、Brackets、Sublime Text;小程序開發工具:微信開發者工具、支付寶小程序開發者工具、百度智能小程序IDE、頭條小程序開發者工具、Taro。

See all articles