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

为微信公众平台加入搜索周围地方的功能

2013-09-07 10:25 113 查看
利用百度地图API,根据用户发送的位置,检测周围有什么,例如:餐厅,商场,便利店等等

private void ResponseMsg(string weixinXML)
{
string key=string.Empty;
try
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(weixinXML);
string msgType = dx.MsgType;
if (msgType.Equals("location"))
{
//处理位置消息
LocationType lt = dx.NewLocationType(dx.Xn);
Response.Write(Utils.GetYourNear(lt.Location_Y, lt.Location_X, "餐厅", lt.FromUserName, lt.ToUserName));
}
}


获得的方法

public static string GetYourNear(string location_X, string location_Y,string what, string UserOpenId, string DevOpenId)
{

string url = "http://api.map.baidu.com/telematics/v3/local?location=" + location_X + "," + location_Y + "&keyWord=" + HttpContext.Current.Server.UrlEncode(what) + "&output=xml&ak=" + BaiduKey;
WebRequest webRequest = WebRequest.Create(url);
HttpWebRequest request = webRequest as HttpWebRequest;
request.Method = "GET";
request.ContentType = "application/json; charset=utf-8";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.2; rv:20.0) Gecko/20100101 Firefox/20.0";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string tianqiXML = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(tianqiXML);
XmlNode parentNode = xmlDoc.DocumentElement;
if (parentNode["total"].InnerText == "0")
{
return ResponseWeixin.ResponseText("您好,您的周围不存在【" + what + "】!\n您可重新尝试!", UserOpenId, DevOpenId);
}
XmlNode aaa = parentNode["poiList"];
// XmlNode rList = aaa["poiList"];
List<news> items = new List<news>();
int i=1;
foreach (XmlNode xn in aaa)
{
news item = new news();
item.Title = xn["name"].InnerText + " 电话:" + xn["telephone"].InnerText + " 距离:" + xn["distance"].InnerText + "米";
XmlNode bbb = xn["location"];
if(i==1)
{
item.PicUrl = "http://api.map.baidu.com/staticimage?center=" + bbb["lng"].InnerText + "," + bbb["lat"].InnerText + "&width=320&height=160&zoom=18&markers=" + bbb["lng"].InnerText + "," + bbb["lat"].InnerText + "&markerStyles=l,A";
}else
{
item.PicUrl = "http://api.map.baidu.com/staticimage?center=" + bbb["lng"].InnerText + "," + bbb["lat"].InnerText + "&width=80&height=80&zoom=15&markers=" + bbb["lng"].InnerText + "," + bbb["lat"].InnerText + "&markerStyles=l,A";
}
i++;

// item.PicUrl = ResponseWeixin.GetWebUrl() + "/weixin/png/logo.png";
items.Add(item);
}
return ResponseWeixin.ResponseSubscribe(items, UserOpenId, DevOpenId);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: