Home  >  Article  >  WeChat Applet  >  Detailed explanation of geographical location examples for developing WeChat public platform with .NET

Detailed explanation of geographical location examples for developing WeChat public platform with .NET

Y2J
Y2JOriginal
2017-04-22 14:59:401699browse

This article mainly provides a detailed analysis of the geographical location .Net code for the development of the WeChat public platform. Interested friends can refer to it

There are two types of geographical location involved in the WeChat public platform Situation:
     First, I send a self-selected geographical location to WeChat, and then WeChat can automatically feedback the response information.
      Second, let WeChat obtain our GPS positioning address location and feedback the response information.
                                                                                                                                                                                                      # The above attributes:

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

This way when we send a geographical location information, we can feedback the response information. It is worth mentioning that the geographical information location here does not require authorization, because the geographical information location sent by ourselves is not necessarily our real location. We can make any selection on the input interface without involving privacy.
Of course, if we create a function similar to "Near Me", we must meet two conditions: enable the function of obtaining user geographical information in the WeChat public account. Second, the user himself allows the WeChat public account to obtain my location when following WeChat. This requires the use of the second situation we introduced to you at the beginning of the article. According to WeChat's explanation, when a session starts (that is, when entering the conversation interface), it is obtained first, and then automatically obtained every five seconds. That is to say, what is triggered when obtaining the user's location information is not a "conversation between you and me", but a special event that occurs every five seconds. It is defined here that MsgType is "event", and in order to distinguish it from other "events", its EventName (actually officially called event) is "LOCATION" (in capital letters).
Next I still need to modify our wxmessage class according to the format of WeChat:

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

When MsgType is event, we used the menu event before, now we need to add its EventName as "LOCATION" The code snippet, because there are no other events involved yet, I will use else later. I will write the code more standardized later. Here, assign values ​​to the three newly added attributes, and then modify the Onload function

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

Okay, done, so that when you open your WeChat "Get User Location Information", the WeChat platform will remind you, Whether to obtain it only for the first time after entering the session, or to obtain it every 5 seconds, if you choose the latter, you will see that geographical location information will be fed back to you every 5 seconds.
This needs to be noticed very much: I think there is no problem as I think, but how can I get the information, that is because when I enters the session, you will see that your mobile phone GPS is searching and positioning in GPS. Previously, the content would not be visible. It can be understood that when you GPS search and locate, the event of obtaining the user's location information will be triggered. This is not what I imagined that the approximate location can be obtained through base station positioning. This requires developers to pay attention. I just spent a long time doing it. , when I went out, I checked the location of my phone and accidentally saw the reply, and then I suddenly realized it.
Having said that, you may ask what is the use of only knowing the latitude and longitude coordinates? It's not a specific location. In fact, we can use a variety of methods to know the detailed information of the location. For example, we can use BaiduMap API's reverse address analysis to guide the coordinates in which city, which street, etc., and even know the nearby situation. No more here. Having said that, I will talk to you about BaiduMap

when I have the opportunity in the future.

The above is the detailed content of Detailed explanation of geographical location examples for developing WeChat public platform with .NET. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn