Geographic location of WeChat public platform development. Net code parsing, geographic location. net

Source: Internet
Author: User

Geographic location of public platform development. Net code parsing, geographic location. net

There are two geographical locations involved in the public platform:
First,I can send a custom location to automatically feedback the response information.
Second,Let's get the GPS location and feedback the response information.
First, let's look at the first one. In addition to sending texts, images, and audios, there is also a geographic location. According to XML Information that accepts geographic information, we need to modify the previous wxmessage class with several 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;} pu Blic string Scale {get; set;} public string Label {get; set ;}where Location_X represents the latitude, Location_Y represents the longitude, and Scale represents the scaling ratio, label indicates the location description and accepted text. For a voice message, the MsgType of the Geographic Information is "location". Modify the previous GetWxMessage () function and the message processing in 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. S ElectSingleNode ("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 Welcome Beijing Yongjie youxin Technology Co., Ltd./: rose \ n directly reply" hello ""; res = sendTextMessage (wx, content);} else {content = "QR code parameter: \ n" + wx. eventKey. replace ("qrscene _", ""); res = sendTextMessage (wx, content) ;}} else if (! String. isNullOrEmpty (wx. eventName) & wx. eventName. toLower () = "scan") {string str = "QR code parameter: \ n" + wx. eventKey; res = sendTextMessage (wx, str);} else if (! String. isNullOrEmpty (wx. eventName) & wx. eventName. trim () = "CLICK") {if (wx. eventKey = "HELLO") res = sendTextMessage (wx, "HELLO, welcome to Beijing Yongjie youxin Technology Co., Ltd. public platform! ");} Else {WriteLog (wx. msgType); if (wx. msgType = "text" & wx. content = "hello") {res = sendTextMessage (wx, "Hello, welcome to Beijing Yongjie youxin Technology Co., Ltd. public platform! ");} Else if (wx. msgType = "voice") {res = sendTextMessage (wx, wx. recognition);} else if (wx. msgType = "location") {res = sendTextMessage (wx, "Your sending location is:" + wx. label + "; latitude:" + wx. location_X + "; longitude:" + wx. location_Y + "; scaling ratio:" + wx. scale);} else {res = sendTextMessage (wx, "Hello, failed to recognize the message! ") ;}} Response. Write (res );}

In this way, when we send a geographic location information, we can feed back the response information. It is worth mentioning that no authorization is required for the location of the geographical information here, because the location of the geographical information sent by yourself is not necessarily your actual location. We can choose any location on the input interface, privacy is not involved.
Of course, if we create a feature similar to "near me", we must have two conditions to enable the feature of Obtaining user geographic information in the public number. Second, the user can allow the public number to retrieve my location when paying attention to it. This requires the second situation we introduced at the beginning of the article. According to the explanation, when a session starts (that is, when the dialog page is displayed), it is obtained first and then automatically obtained every five seconds. That is to say, when obtaining user location information, the trigger is not a "one-word conversation", but a special event that starts every five seconds. This is defined as MsgType as "event". to distinguish it from other "Events", its EventName (officially called event) is "LOCATION" (in upper case ).
Below I still need to modify our wxmessage class according to the format:

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 longpolling {get; set;} public string Precision {get; set ;}transform the GetWxMessage () and OnLoad functions: 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. longpolling = xml. selectSingleNode ("xml "). selectSingleNode ("longpolling "). 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 a menu event. Now we need to add a code segment whose EventName is "LOCATION, because there are no other events involved, I will use else later, and I will standardize the code later. Assign values to the three new attributes here, 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 Welcome Beijing Yongjie youxin Technology Co., Ltd./: rose \ n directly reply" hello ""; res = sendTextMessage (wx, content);} else {content = "QR code parameter: \ n" + wx. eventKey. replace ("qrscene _", ""); res = sendTextMessage (wx, content) ;}} else if (! String. isNullOrEmpty (wx. eventName) & wx. eventName. toLower () = "scan") {string str = "QR code parameter: \ n" + wx. eventKey; res = sendTextMessage (wx, str);} else if (! String. isNullOrEmpty (wx. eventName) & wx. eventName. trim () = "CLICK") {if (wx. eventKey = "HELLO") res = sendTextMessage (wx, "HELLO, welcome to Beijing Yongjie youxin Technology Co., Ltd. public platform! ");} Else if (! String. isNullOrEmpty (wx. eventName) & wx. eventName. trim () = "LOCATION") {res = sendTextMessage (wx, "Your LOCATION is longitude:" + wx. latitude + ", dimension:" + wx. longpolling + ", geographic Longitude:" + wx. precision);} else {if (wx. msgType = "text" & wx. content = "hello") {res = sendTextMessage (wx, "Hello, welcome to Beijing Yongjie youxin Technology Co., Ltd. public platform! ");} Else if (wx. msgType = "voice") {res = sendTextMessage (wx, wx. recognition);} else if (wx. msgType = "location") {res = sendTextMessage (wx, "Your sending location is:" + wx. label + "; latitude:" + wx. location_X + "; longitude:" + wx. location_Y + "; scaling ratio:" + wx. scale);} else {res = sendTextMessage (wx, "Hello, failed to recognize the message! ") ;}} Response. Write (res );}

Now, when you enable "get user location information", the platform will remind you whether to access the session for the first time or every 5 seconds, if you select the latter, you will see that every 5 seconds, you will be given a geographical location information.
It should be noted that I think it is okay, but I cannot get the information because when I enter the session, you will see that your mobile phone GPS is searching. before GPS positioning, you will not see the content. It can be understood in this way that only when you search for a location by GPS will an event be triggered to obtain the user's location information. This is not what I think the base station positioning can also obtain the approximate location, this requires developers to note that I just spent a long time waiting for me to go out and locate the phone and accidentally see the response.
Speaking of this, you may ask only what is the use of latitude and longitude coordinates? It is not a specific location. Otherwise, we can use multiple methods to know detailed location information. For example, we can use the address reverse resolution of the BaiduMap API to guide the coordinates in that city, that street, and so on, I can even know the situation nearby, so I will not talk about it here. I will have a chance to talk with you about BaiduMap in the future.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.