微信公共平台中涉及到地理位置的有两种情况:
       第一、我发送一个自选的地理位置给微信,然后微信可以自动反馈响应的信息。
       第二、让微信获取我们GPS定位地址位置,反馈响应的信息。

首先我们先来看第一种,在微信中除了可以发文本,图片,语音等还有一个信息就是地理位置,按照微信接受地理信息的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);
}

这样当我们发送一个地理位置信息的时候就可以反馈响应的信息了。值得一提的是:这里的地理信息位置无需授权,因为自己发送的地理信息位置不一定是自己的真实位置,我们可以在输入界面进行任意选择,不会涉及隐私。
       当然如果我们像制作类似于“我附近”的功能的时候,就必须有两个条件,在微信公共号中开启获取用户地理信息的功能。第二,用户自己在关注微信的时候允许微信公共号获取我的位置。这就需要用到我们在文章开始的时候给大家介绍的第二种情况了。按照微信的解释,当一个会话开始的时候(也就是说进入对话界面的时候),首先获取一下,然后每个五秒自动获取一次。也就是就是说获得用户位置信息的时候触发的不是“你一言我一语的对话”,而是一个特殊的事件,每格五秒出发一次。这里被定义为MsgType为“event”,而为了区别于其他的“event”,他的EventName(其实官方叫做event)为“LOCATION”(大写哦)。
       下面我依然需要按照微信的格式修改我们的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为event的时候我们之前用到的是菜单的事件,现在我们需要加入其EventName为"LOCATION"的代码段,因为现在还没有涉及其他的event我后面就用else好了,后面我会把代码写的规范些。在这里分别给新增的三个属性赋值,然后修改一下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);
}

好了,完成,这样当你开启你的微信“获得用户位置信息”的时候微信平台会提醒你,是仅进入会话第一次获取,还是每个5秒获取一次,如果你选择了后者,你就会看到,每5秒会反馈给你一个地理位置的信息。
       这里面需要非常注意的是:我按照这样认为没有问题了,但是怎么也获得不了信息,那是因为我在进入会话的时候,你会看到你的手机GPS在搜索,在GPS定位以前,是不会看到内容的。可以这样理解,当你GPS搜索定位后,才会触发获得用户位置信息的事件,这一点并不是我想象的通过基站定位也可以获得大致的位置,这一点需要开发者注意,我就是弄了半天,等我出门儿,手机定位了无意间看到了回复,这才恍然大悟。
       说到这里可以各位会问只知道经纬度坐标有什么用?又不是具体位置。其实不然,我们可以使用多种方法知道位置详细的信息,例如我们可以通过BaiduMap API的地址反向解析指导这个坐标在那个城市,那个街道等内容,甚至可以知道附近的情况,这里就不再多说了,以后有机会和大家一起来谈谈BaiduMap

微信公共服务平台开发(.Net 的实现)10-------地理位置的更多相关文章

  1. 微信公共服务平台开发(.Net 的实现)13-------网页授权(下 :C#代码的实现 )

    接着上次的理论,我们这次来研究用代码实现“网页授权获取用户基本信息”,首先我们需要一个链接指向微信的授权页面,在微信开发平台中已经说了,这个链接必须在微信客户端中打开,那么我们就干脆使用文本消息来完成 ...

  2. 微信公共服务平台开发(.Net的实现)1 认证“成为开发者”

    http://www.cnblogs.com/freeliver54/p/3725979.html http://www.it165.net/pro/html/201402/9459.html 这些代 ...

  3. 微信公共服务平台开发(.Net 的实现)9-------处理二维码

    今天我们来共同学习一下微信公共服务平台中一个重要内容---二维码扫描.众所周知二维码目前应用范围很广,在这里不再叙述背景了,但是值得一提的是目前大家手机上面应用的二维码扫描工具是支持的都是QR码和PD ...

  4. 微信公共服务平台开发(.Net 的实现)11-------客服消息(定项消息推送 重要的OPENID)

    这次我们来一起研究一下“客服消息”,首先明确一下“客服消息”的概念.这不同于之前的“被动响应消息”,也就是说并不是之前“你一言我一语的即时响应”,可能在某种情况下你需要给不同的人主动发送消息,例如你的 ...

  5. 微信公共服务平台开发(.Net 的实现)6-------自定义菜单

    用户自定义菜单制作时,需要用到access_token,我们直接使用前面讲解的IsExistAccess_Token()函数.我理解的微信公共平台里面菜单分为button和sub_button,即菜单 ...

  6. 微信公共服务平台开发(.Net 的实现)12-------网页授权(上 :更加深入理解OAuth2.0 )

    我们首先来认识一下OAuth协议吧,这个东西很早就听说过,总觉得离我很远(我的项目用不到这些),但是最近不得不学习一下了.我在网上找了一些解释,认为解释的最好的是这样说的(出处:http://hi.b ...

  7. 微信公共服务平台开发(.Net 的实现)8-------处理图片(上传下载发送)

    举个例子,有人对着我们的公共微信号拍个照片发送过来,然后我们处理这个照片,比如进行ocr识别字(随后就会降到这个例子),或者人脸识别,或者拍照取证等,这些功能都是相当有用的.那么我们现在就要分析一下这 ...

  8. 微信公共服务平台开发(.Net 的实现)7-------发送图文消息

    之前我们讲过让微信发送给我们普通的文本信息,下面我们来看看如何发送图文信息,需要注意的是这里说的是,让微信发给我们,而不是我们拍个图片发给微信处理,我们上传图片在以后的章节介绍.下面是发送图文消息的函 ...

  9. 微信公共服务平台开发(.Net 的实现)2-------获得ACCESSTOKEN

    成为了开发者之后微信平台会给您appid和secret,在订阅号中是没有的,所以因该申请一下服务号 有了ACCESSTOKEN才能做添加菜单,上传/下载图片等功能 private string Get ...

随机推荐

  1. 二叉树的基本操作(C)

    实现二叉树的创建(先序).递归及非递归的先.中.后序遍历 请按先序遍历输入二叉树元素(每个结点一个字符,空结点为'='): ABD==E==CF==G== 先序递归遍历: A B D E C F G ...

  2. Protocol Buffer详解

    1.Protocol Buffer 概念 Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 ...

  3. 精选PSD素材下载周刊【Goodfav PSD 20130720】

    我们每周精选来自Goodfav PSD的免费PSD素材,有兴趣的朋友尤其是做设计工作的,不妨收藏或者下载. 这些现成的PSD素材能给我们一些相关的灵感,从而提高工作的效率. 1.File Upload ...

  4. golang实现ios推送

    生成pem文件 打开Keychain Access 导出推送证书和私钥 推送证书 cert.p12 私钥 key.p12 导出.pem文件 转换推送证书 openssl pkcs12 -clcerts ...

  5. Cocos2d-JS v3.0 alpha不支持cocos2d-x的Physics integration

    cocos2d-x 3.0新的Physics integration,把chipmunk和Box2D封装到引擎内部 auto scene = Scene::createWithPhysics(); s ...

  6. 基于jquery的表格动态创建,自动绑定,自动获取值

    最近刚加入GUT项目,学习了很多其他同事写的代码,感觉受益匪浅. 在GUT项目中,经常会碰到这样一个问题:动态生成表格,包括从数据库中读取数据,并绑定在表格中,以及从在页面上通过jQuery新增删除表 ...

  7. linux极点五笔无法输入词组_ibus设置

    菜鸟学linux——用的是ubuntu 不知道是不是按个哪些快捷键,极点五笔突然无法输入词组.那个抓狂啊 没关系,设置一下就ok 第一步:右上角输入法,右键——>首选项——>常规——> ...

  8. CCS5 编译器手动设置dsp支持可变参数宏等问题

    IDE:CSS5.4,compiler不支持可变参数宏.需要手动设置编译器相关选项: Language Option->Language Mode —>no strict ANSI. 1. ...

  9. HDU1002大数加法

    大数加法 c++版: #include <map> #include <set> #include <stack> #include <queue> # ...

  10. java解析属性文件

    -----------------------解析属性文件----------------------------- /**   * 获取src下属性文件   * @param params   * ...