Home WeChat Applet WeChat Development 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

Apr 22, 2017 pm 02:59 PM
.net geographical location WeChat

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);
   }
Copy after login

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;
   }
Copy after login

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);
   }
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is H5 page production a front-end development? Is H5 page production a front-end development? Apr 05, 2025 pm 11:42 PM

Yes, H5 page production is an important implementation method for front-end development, involving core technologies such as HTML, CSS and JavaScript. Developers build dynamic and powerful H5 pages by cleverly combining these technologies, such as using the <canvas> tag to draw graphics or using JavaScript to control interaction behavior.

The difference between H5 and mini-programs and APPs The difference between H5 and mini-programs and APPs Apr 06, 2025 am 10:42 AM

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

What is the difference between H5 page production and WeChat applets What is the difference between H5 page production and WeChat applets Apr 05, 2025 pm 11:51 PM

H5 is more flexible and customizable, but requires skilled technology; mini programs are quick to get started and easy to maintain, but are limited by the WeChat framework.

What should I do if the company's security software conflicts with applications? How to troubleshoot HUES security software causes common software to fail to open? What should I do if the company's security software conflicts with applications? How to troubleshoot HUES security software causes common software to fail to open? Apr 01, 2025 pm 10:48 PM

Compatibility issues and troubleshooting methods for company security software and application. Many companies will install security software in order to ensure intranet security. However, security software sometimes...

How to solve the problem of JS resource caching in enterprise WeChat? How to solve the problem of JS resource caching in enterprise WeChat? Apr 04, 2025 pm 05:06 PM

Discussion on the JS resource caching issue of Enterprise WeChat. When upgrading project functions, some users often encounter situations where they fail to successfully upgrade, especially in the enterprise...

How to choose H5 and applets How to choose H5 and applets Apr 06, 2025 am 10:51 AM

The choice of H5 and applet depends on the requirements. For applications with cross-platform, rapid development and high scalability, choose H5; for applications with native experience, rich functions and platform dependencies, choose applets.

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Does mysql need the internet Does mysql need the internet Apr 08, 2025 pm 02:18 PM

MySQL can run without network connections for basic data storage and management. However, network connection is required for interaction with other systems, remote access, or using advanced features such as replication and clustering. Additionally, security measures (such as firewalls), performance optimization (choose the right network connection), and data backup are critical to connecting to the Internet.

See all articles