您的位置:首页 > 移动开发 > 微信开发

.net 开发微信公众平台(四)-----地理位置

2014-08-08 11:47 155 查看
番外篇: 哈哈,我也有番外篇啦

接受地理位置



接收地理位置的后台代码:(在default页面上的)

public string Doevent(string xml)
{
//***前面省略***

//接收地理位置
if (MsgType.Equals("location"))
{
string city = GetMapInfo(element.SelectSingleNode("Location_X").InnerText, element.SelectSingleNode("Location_Y").InnerText);
if (city == "0")
{
rexml = xmlhelper.CreatXYXml(FormUserName, city);
}
else
{
rexml = xmlhelper.CreatXYXml(FormUserName, city);
}
}
还有:
///<summary>
///调用百度地图,返回坐标信息
///</summary>
///<param name="y">经度</param>
/// <param name="x">纬度</param>
///<returns></returns>
public string GetMapInfo(string x, string y)
{
try
{
string res = string.Empty;
string parame = string.Empty;
string url = "http://maps.googleapis.com/maps/api/geocode/xml";
parame = "latlng=" + x + "," + y + "&language=zh-CN&sensor=false"; //此key为个人申请
res = webRequestPost(url, parame);

XmlDocument doc = new XmlDocument();

doc.LoadXml(res);
XmlElement rootElement = doc.DocumentElement;
string Status = rootElement.SelectSingleNode("status").InnerText;
if (Status == "OK")
{
//仅获取城市
XmlNodeList xmlResults = rootElement.SelectSingleNode("/GeocodeResponse").ChildNodes;
for (int i = 0; i < xmlResults.Count; i++)
{
XmlNode childNode = xmlResults[i];
if (childNode.Name == "status")
{
continue;
}
string city = "0";
for (int w = 0; w < childNode.ChildNodes.Count; w++)
{
for (int q = 0; q < childNode.ChildNodes[w].ChildNodes.Count; q++)
{
XmlNode childeTwo = childNode.ChildNodes[w].ChildNodes[q];

if (childeTwo.Name == "long_name")
{
city = childeTwo.InnerText;
}
else if (childeTwo.InnerText == "locality")
{
return city;
}
}
}
return city;
}
}
}
catch (Exception ex)
{
WriteTxt("地图异常:" + ex.Message.ToString() + "Struck:" + ex.StackTrace.ToString());
return "0";
}
return "0";
}

/// <summary>
/// Post 提交调用抓取
/// </summary>
/// <param name="url">提交地址</param>
/// <param name="param">参数</param>
/// <returns>string</returns>
public string webRequestPost(string url, string param)
{
byte[] bs = System.Text.Encoding.UTF8.GetBytes(param);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url + "?" + param);
req.Method = "Post";
req.Timeout = 120 * 1000;
req.ContentType = "application/x-www-form-urlencoded;";
req.ContentLength = bs.Length;

using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
reqStream.Flush();
}
using (WebResponse wr = req.GetResponse())
{
//在这里对接收到的页面内容进行处理
Stream strm = wr.GetResponseStream();
StreamReader sr = new StreamReader(strm, System.Text.Encoding.UTF8);
string line;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
while ((line = sr.ReadLine()) != null)
{
sb.Append(line + System.Environment.NewLine);
}
sr.Close();
strm.Close();
return sb.ToString();
}
}

在XmlHelper上建一个方法:

public class XmlHelper
{
//**前面省略**

//回复地理位置消息
public string CreatXYXml(string ToUserName, string city)
{
string rexml = "<xml><ToUserName>" + ToUserName + "</ToUserName><FromUserName>" + FromUserName + "</FromUserName>";
rexml += "<CreateTime>" + DateTime.Now.Ticks + "</CreateTime><MsgType>text</MsgType>";
rexml += "<Content>你以为我不知道你在【" + city + "】?</Content><FuncFlag>0</FuncFlag></xml>";
return rexml;
}                                                                                                                                                     }
搞定,技术有限,欢迎指正.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  微信 .net