天天看點

微信-地理位置

 微信公共平台中涉及到地理位置的有兩種情況:

       第一、我發送一個自選的地理位置給微信,然後微信可以自動回報響應的資訊。

       第二、讓微信擷取我們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; }

}      
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;
}      
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);
}