微信公共服务平台开发(.Net 的实现)10-------地理位置

来源:互联网 发布:linux 编译内核教程 编辑:程序博客网 时间:2024/05/08 14:52
微信公共平台中涉及到地理位置的有两种情况:
       第一、我发送一个自选的地理位置给微信,然后微信可以自动反馈响应的信息。
       第二、让微信获取我们GPS定位地址位置,反馈响应的信息。

       首先我们先来看第一种,在微信中除了可以发文本,图片,语音等还有一个信息就是地理位置,按照微信接受地理信息的XML信息,我们需要改造一下之前的wxmessage类加上几个属性:

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. class wxmessage    
  2.    {    
  3.        public string FromUserName { getset; }    
  4.        public string ToUserName { getset; }    
  5.         public string MsgType { getset; }    
  6.         public string EventName { getset; }    
  7.         public string Content { getset; }  
  8.         public string Recognition { getset; }  
  9.         public string MediaId { getset; }  
  10.         public string EventKey { getset; }   
  11.         public string Location_X { getset; }  
  12.         public string Location_Y { getset; }  
  13.         public string Scale { getset; }  
  14.         public string Label { getset; }  
  15.   
  16.     }  
       其中Location_X代表纬度,Location_Y代表经度,Scale代表缩放比例,Label代表位置的描述
       和接受文本,语音消息一下样,地理信息的MsgType为“location”,修改一下之前的GetWxMessage()函数和OnLoad里面的消息处理:
[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. private wxmessage GetWxMessage()  
  2.      {  
  3.          wxmessage wx = new wxmessage();  
  4.          StreamReader str = new StreamReader(Request.InputStream, System.Text.Encoding.UTF8);  
  5.          XmlDocument xml = new XmlDocument();  
  6.          xml.Load(str);  
  7.          wx.ToUserName = xml.SelectSingleNode("xml").SelectSingleNode("ToUserName").InnerText;  
  8.          wx.FromUserName = xml.SelectSingleNode("xml").SelectSingleNode("FromUserName").InnerText;  
  9.          wx.MsgType = xml.SelectSingleNode("xml").SelectSingleNode("MsgType").InnerText;  
  10.          if (wx.MsgType.Trim() == "text")  
  11.          {  
  12.              wx.Content = xml.SelectSingleNode("xml").SelectSingleNode("Content").InnerText;  
  13.          }  
  14.          if (wx.MsgType.Trim() == "location")  
  15.          {  
  16.              wx.Location_X = xml.SelectSingleNode("xml").SelectSingleNode("Location_X").InnerText;  
  17.              wx.Location_Y = xml.SelectSingleNode("xml").SelectSingleNode("Location_Y").InnerText;  
  18.              wx.Scale = xml.SelectSingleNode("xml").SelectSingleNode("Scale").InnerText;  
  19.              wx.Label = xml.SelectSingleNode("xml").SelectSingleNode("Label").InnerText;  
  20.   
  21.          }  
  22.          if (wx.MsgType.Trim() == "event")  
  23.          {  
  24.              wx.EventName = xml.SelectSingleNode("xml").SelectSingleNode("Event").InnerText;  
  25.              wx.EventKey = xml.SelectSingleNode("xml").SelectSingleNode("EventKey").InnerText;  
  26.          }  
  27.          if (wx.MsgType.Trim() == "voice")  
  28.          {  
  29.              wx.Recognition = xml.SelectSingleNode("xml").SelectSingleNode("Recognition").InnerText;  
  30.          }  
  31.            
  32.          return wx;  
  33.      }  
[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. protected void Page_Load(object sender, EventArgs e)  
  2.   {  
  3.       wxmessage wx = GetWxMessage();  
  4.       string res = "";  
  5.   
  6.   
  7.       if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "subscribe")  
  8.       {  
  9.           string content = "";  
  10.           if (!wx.EventKey.Contains("qrscene_"))  
  11.           {  
  12.               content = "/:rose欢迎北京永杰友信科技有限公司/:rose\n直接回复“你好”";  
  13.               res = sendTextMessage(wx, content);  
  14.           }  
  15.           else  
  16.           {  
  17.               content = "二维码参数:\n" + wx.EventKey.Replace("qrscene_""");  
  18.               res = sendTextMessage(wx, content);  
  19.           }  
  20.       }  
  21.   
  22.       else if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.ToLower() == "scan")  
  23.       {  
  24.           string str = "二维码参数:\n" + wx.EventKey;  
  25.           res = sendTextMessage(wx, str);  
  26.       }  
  27.       else if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "CLICK")  
  28.       {  
  29.           if(wx.EventKey=="HELLO")  
  30.               res = sendTextMessage(wx, "你好,欢迎使用北京永杰友信科技有限公司公共微信平台!");  
  31.       }  
  32.       else  
  33.       {  
  34.           WriteLog(wx.MsgType);  
  35.           if (wx.MsgType == "text" && wx.Content == "你好")  
  36.           {  
  37.               res = sendTextMessage(wx, "你好,欢迎使用北京永杰友信科技有限公司公共微信平台!");  
  38.           }  
  39.           else if (wx.MsgType == "voice")  
  40.           {  
  41.               res = sendTextMessage(wx, wx.Recognition);  
  42.           }  
  43.           else if (wx.MsgType == "location")  
  44.           {  
  45.               res = sendTextMessage(wx, "您发送的位置是:" + wx.Label + ";纬度是:" + wx.Location_X + ";经度是:" + wx.Location_Y + ";缩放比例为:" + wx.Scale);  
  46.           }  
  47.           else  
  48.           {  
  49.               res = sendTextMessage(wx, "你好,未能识别消息!");  
  50.           }  
  51.       }  
  52.   
  53.       Response.Write(res);  
  54.   }  
       这样当我们发送一个地理位置信息的时候就可以反馈响应的信息了。值得一提的是:这里的地理信息位置无需授权,因为自己发送的地理信息位置不一定是自己的真实位置,我们可以在输入界面进行任意选择,不会涉及隐私。
       当然如果我们像制作类似于“我附近”的功能的时候,就必须有两个条件,在微信公共号中开启获取用户地理信息的功能。第二,用户自己在关注微信的时候允许微信公共号获取我的位置。这就需要用到我们在文章开始的时候给大家介绍的第二种情况了。按照微信的解释,当一个会话开始的时候(也就是说进入对话界面的时候),首先获取一下,然后每个五秒自动获取一次。也就是就是说获得用户位置信息的时候触发的不是“你一言我一语的对话”,而是一个特殊的事件,每格五秒出发一次。这里被定义为MsgType为“event”,而为了区别于其他的“event”,他的EventName(其实官方叫做event)为“LOCATION”(大写哦)。
       下面我依然需要按照微信的格式修改我们的wxmessage类:
[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. class wxmessage    
  2.   {    
  3.       public string FromUserName { getset; }    
  4.       public string ToUserName { getset; }    
  5.        public string MsgType { getset; }    
  6.        public string EventName { getset; }    
  7.        public string Content { getset; }  
  8.        public string Recognition { getset; }  
  9.        public string MediaId { getset; }  
  10.        public string EventKey { getset; }   
  11.        public string Location_X { getset; }  
  12.        public string Location_Y { getset; }  
  13.        public string Scale { getset; }  
  14.        public string Label { getset; }  
  15.        public string Latitude { getset; }  
  16.        public string Longitude { getset; }  
  17.        public string Precision { getset; }  
  18.   
  19.    }  
       改造一下GetWxMessage()函数和OnLoad函数:
[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. private wxmessage GetWxMessage()  
  2.      {  
  3.          wxmessage wx = new wxmessage();  
  4.          StreamReader str = new StreamReader(Request.InputStream, System.Text.Encoding.UTF8);  
  5.          XmlDocument xml = new XmlDocument();  
  6.          xml.Load(str);  
  7.          wx.ToUserName = xml.SelectSingleNode("xml").SelectSingleNode("ToUserName").InnerText;  
  8.          wx.FromUserName = xml.SelectSingleNode("xml").SelectSingleNode("FromUserName").InnerText;  
  9.          wx.MsgType = xml.SelectSingleNode("xml").SelectSingleNode("MsgType").InnerText;  
  10.          WriteLog("MsgType:"+wx.MsgType);  
  11.          if (wx.MsgType.Trim() == "event")  
  12.          {  
  13.              wx.EventName = xml.SelectSingleNode("xml").SelectSingleNode("Event").InnerText;  
  14.              WriteLog(wx.EventName);  
  15.              if (wx.EventName.ToUpper() == "LOCATION")  
  16.              {  
  17.                  wx.Latitude = xml.SelectSingleNode("xml").SelectSingleNode("Latitude").InnerText;  
  18.                  wx.Longitude = xml.SelectSingleNode("xml").SelectSingleNode("Longitude").InnerText;  
  19.                  wx.Precision = xml.SelectSingleNode("xml").SelectSingleNode("Precision").InnerText;  
  20.              }  
  21.              else  
  22.              {  
  23.                  wx.EventKey = xml.SelectSingleNode("xml").SelectSingleNode("EventKey").InnerText;  
  24.              }  
  25.          }  
  26.          if (wx.MsgType.Trim() == "text")  
  27.          {  
  28.              wx.Content = xml.SelectSingleNode("xml").SelectSingleNode("Content").InnerText;  
  29.          }  
  30.          if (wx.MsgType.Trim() == "location")  
  31.          {  
  32.              wx.Location_X = xml.SelectSingleNode("xml").SelectSingleNode("Location_X").InnerText;  
  33.              wx.Location_Y = xml.SelectSingleNode("xml").SelectSingleNode("Location_Y").InnerText;  
  34.              wx.Scale = xml.SelectSingleNode("xml").SelectSingleNode("Scale").InnerText;  
  35.              wx.Label = xml.SelectSingleNode("xml").SelectSingleNode("Label").InnerText;  
  36.   
  37.          }  
  38.            
  39.          if (wx.MsgType.Trim() == "voice")  
  40.          {  
  41.              wx.Recognition = xml.SelectSingleNode("xml").SelectSingleNode("Recognition").InnerText;  
  42.          }  
  43.            
  44.          return wx;  
  45.      }  
       当MsgType为event的时候我们之前用到的是菜单的事件,现在我们需要加入其EventName为"LOCATION"的代码段,因为现在还没有涉及其他的event我后面就用else好了,后面我会把代码写的规范些。在这里分别给新增的三个属性赋值,然后修改一下Onload函数
[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. protected void Page_Load(object sender, EventArgs e)  
  2.     {  
  3.   
  4.         wxmessage wx = GetWxMessage();  
  5.         string res = "";  
  6.   
  7.   
  8.         if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "subscribe")  
  9.         {  
  10.             string content = "";  
  11.             if (!wx.EventKey.Contains("qrscene_"))  
  12.             {  
  13.                 content = "/:rose欢迎北京永杰友信科技有限公司/:rose\n直接回复“你好”";  
  14.                 res = sendTextMessage(wx, content);  
  15.             }  
  16.             else  
  17.             {  
  18.                 content = "二维码参数:\n" + wx.EventKey.Replace("qrscene_""");  
  19.                 res = sendTextMessage(wx, content);  
  20.             }  
  21.         }  
  22.   
  23.         else if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.ToLower() == "scan")  
  24.         {  
  25.             string str = "二维码参数:\n" + wx.EventKey;  
  26.             res = sendTextMessage(wx, str);  
  27.         }  
  28.         else if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "CLICK")  
  29.         {  
  30.             if(wx.EventKey=="HELLO")  
  31.                 res = sendTextMessage(wx, "你好,欢迎使用北京永杰友信科技有限公司公共微信平台!");  
  32.         }  
  33.         else if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "LOCATION")  
  34.         {  
  35.             res = sendTextMessage(wx, "您的位置是经度:" + wx.Latitude + ",维度是:" + wx.Longitude+",地理经度为:"+wx.Precision);  
  36.         }  
  37.         else  
  38.         {  
  39.             if (wx.MsgType == "text" && wx.Content == "你好")  
  40.             {  
  41.                 res = sendTextMessage(wx, "你好,欢迎使用北京永杰友信科技有限公司公共微信平台!");  
  42.             }  
  43.             else if (wx.MsgType == "voice")  
  44.             {  
  45.                 res = sendTextMessage(wx, wx.Recognition);  
  46.             }  
  47.             else if (wx.MsgType == "location")  
  48.             {  
  49.                 res = sendTextMessage(wx, "您发送的位置是:" + wx.Label + ";纬度是:" + wx.Location_X + ";经度是:" + wx.Location_Y + ";缩放比例为:" + wx.Scale);  
  50.             }  
  51.             else  
  52.             {  
  53.                 res = sendTextMessage(wx, "你好,未能识别消息!");  
  54.             }  
  55.         }  
  56.   
  57.         Response.Write(res);  
  58.     }  
       好了,完成,这样当你开启你的微信“获得用户位置信息”的时候微信平台会提醒你,是仅进入会话第一次获取,还是每个5秒获取一次,如果你选择了后者,你就会看到,每5秒会反馈给你一个地理位置的信息。
       这里面需要非常注意的是:我按照这样认为没有问题了,但是怎么也获得不了信息,那是因为我在进入会话的时候,你会看到你的手机GPS在搜索,在GPS定位以前,是不会看到内容的。可以这样理解,当你GPS搜索定位后,才会触发获得用户位置信息的事件,这一点并不是我想象的通过基站定位也可以获得大致的位置,这一点需要开发者注意,我就是弄了半天,等我出门儿,手机定位了无意间看到了回复,这才恍然大悟。
       说到这里可以各位会问只知道经纬度坐标有什么用?又不是具体位置。其实不然,我们可以使用多种方法知道位置详细的信息,例如我们可以通过BaiduMap API的地址反向解析指导这个坐标在那个城市,那个街道等内容,甚至可以知道附近的情况,这里就不再多说了,以后有机会和大家一起来谈谈BaiduMap

1 0