>  기사  >  위챗 애플릿  >  .NET을 사용하여 WeChat 공개 플랫폼을 개발하기 위한 지리적 위치 예제에 대한 자세한 설명

.NET을 사용하여 WeChat 공개 플랫폼을 개발하기 위한 지리적 위치 예제에 대한 자세한 설명

Y2J
Y2J원래의
2017-04-22 14:59:401650검색

이 기사는 주로 WeChat 공개 플랫폼 개발을 위한 지리적 위치 .Net 코드에 대한 자세한 분석을 제공합니다. 관심 있는 친구는 이를 참조할 수 있습니다.

여기에는 두 가지 유형의 지리적 위치가 관련되어 있습니다. 상황:
먼저 내가 선택한 지리적 위치를 WeChat으로 보내면 WeChat이 자동으로 응답 정보를 피드백할 수 있습니다.
둘째, WeChat이 GPS 위치 주소 위치를 가져오고 응답 정보를 피드백하도록 하세요. 우선 첫 번째를 살펴보겠습니다. 문자, 사진, 음성 등을 보내는 것 외에도 지리적 위치를 허용하는 WeChat의 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);
   }

이렇게 하면 지리적 위치 정보를 보낼 때 응답 정보를 피드백할 수 있습니다. 여기서 지리 정보 위치는 인증이 필요하지 않습니다. 왜냐하면 우리가 보낸 지리 정보 위치가 반드시 개인 정보 보호 없이 입력 인터페이스에서 선택할 수 있는 것은 아니기 때문입니다.

물론 "Near Me"와 유사한 기능을 만들려면 두 가지 조건을 충족해야 합니다. 즉, WeChat 공개 계정에서 사용자 지리 정보를 얻는 기능을 활성화해야 합니다. 둘째, 사용자는 WeChat을 팔로우할 때 WeChat 공개 계정이 내 위치를 얻을 수 있도록 허용합니다. 이를 위해서는 기사 시작 부분에서 소개한 두 번째 상황을 사용해야 합니다. WeChat의 설명에 따르면 세션이 시작되면(즉, 대화 인터페이스에 들어갈 때) 먼저 획득한 다음 5초마다 자동으로 획득합니다. 즉, 사용자의 위치 정보를 획득할 때 트리거되는 것은 '너와 나 사이의 대화'가 아니라 5초마다 발생하는 특별한 이벤트입니다. 여기서는 MsgType이 "event"라고 정의되어 있으며, 이를 다른 "이벤트"와 구별하기 위해 해당 EventName(실제로는 공식적으로 이벤트라고 함)은 "LOCATION"(대문자)입니다.
다음으로 WeChat 형식에 따라 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; }

  }
    改造一下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;
   }

MsgType이 이벤트인 경우 이전에는 메뉴 이벤트를 사용했지만 이제 EventName을 "LOCATION"으로 추가해야 합니다. 코드 조각은 아직 관련된 다른 이벤트가 없기 때문에 나중에 더 표준화된 코드를 작성하겠습니다. 여기에서 새로 추가된 세 가지 속성에 값을 할당한 다음 Onload 함수

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

를 수정하세요. 좋아요, 완료되었습니다. 그러면 WeChat을 열 때 "사용자 위치 정보 얻기"라는 알림이 WeChat 플랫폼에서 표시됩니다. 세션 진입 후 처음으로만 획득할지 아니면 5초마다 획득할지, 후자를 선택하면 5초마다 지리적 위치 정보가 피드백되는 것을 볼 수 있습니다.

여기서 주의할 점은, 이에 따르면 문제가 없다고 생각하는데, 세션에 들어가면 휴대폰의 GPS 검색과 GPS가 보이기 때문에 정보를 얻을 수 없다는 점입니다. 이전에는 콘텐츠가 표시되지 않았습니다. GPS를 검색하고 위치를 찾을 때 사용자의 위치 정보를 얻는 이벤트가 발생한다는 것은 이해할 수 있습니다. 이것은 기지국 측위를 통해 대략적인 위치를 얻을 수 있다고 생각한 것이 아닙니다. 이것은 개발자의 주의가 필요합니다. 그냥 한참을 하다가 밖에 나가서 휴대폰 위치를 확인하다가 우연히 답장을 봤는데 문득 깨달았어요.
그러고보니 위도와 경도 좌표만 알면 무슨 소용이 있겠는가? 특정 위치가 아닙니다. 실제로 다양한 방법을 사용하여 자세한 위치 정보를 알 수 있습니다. 예를 들어 BaiduMap API의 역주소 분석을 사용하여 어느 도시, 어느 거리 등의 좌표를 안내하고 근처 상황까지 알 수 있습니다. 그럼 앞으로 기회가 되면 바이두맵에 대해 말씀드리겠습니다

위 내용은 .NET을 사용하여 WeChat 공개 플랫폼을 개발하기 위한 지리적 위치 예제에 대한 자세한 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.