微信公共平臺中涉及到地理位置的有兩種情況:
第一、我發送一個自選的地理位置給微信,然后微信可以自動反饋響應的信息。
第二、讓微信獲取我們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); }
新聞熱點
疑難解答
圖片精選