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

利用百度地图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);
        }




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值