微信爱帮周边查询

时间:2014-06-24 23:44:22   收藏:0   阅读:642

想做个微信小工具方便自己查找东西,通过上传经纬度即可搜索周边,例如ATM 餐馆之类的

之前接触的爱帮有这个接口,百度的也有.我写的是爱帮的,

/**
 * WxDaoImpl
 * 
 * @author xuyw
 * @email xyw10000@163.com
 * @date 2014-06-22
 */


看到上面api必须输入城市名,我想减少微信上的操作,于是打算用经纬度得到城市,

昨天刚写了这个功能 详情见 http://blog.csdn.net/xuyw10000/article/details/33342489

附上代码

public static List searchFj(String q, String city, String lng, String lat)
			throws Exception {
		List list = new ArrayList();
		String str = HttpUtil.getRequest(SEARCH_FJ + "?from=1&to=9&rc=2",
				"app_key:" + Config.AIBANG_KEY, "city:" + city, "lng:" + lng,
				"lat:" + lat, "q:" + q);
		Document document = DocumentHelper.parseText(str);
		// 得到xml根元素
		Element root = document.getRootElement();
		// result_num表示搜索到的公交路线数
		String result_num = root.element("result_num").getText();
		if (!"0".equals(result_num)) {
			// 获取根节点下的子节点bizs
			Iterator bizs = root.element("bizs").elementIterator();
			while (bizs.hasNext()) {
				Map map = new HashMap();
				Element biz = (Element) bizs.next();
				List<Element> elementList = biz.elements();
				// 遍历所有子节点
				for (Element e : elementList)
					map.put(e.getName(), e.getText());

				list.add(map);
			}
		}
		return list;
	}

	public static void main(String[] args) throws Exception {
		List list = AiBangUtil.searchFj("餐馆", "南昌", "115.958458", "28.696117");
		for (int i = 0; i < list.size(); i++) {
			Map<Object, Object> map = (Map<Object, Object>) list.get(i);
			for (Map.Entry<Object, Object> entry : map.entrySet()) {
				System.out.println(entry.getKey() + " : " + entry.getValue());

			}
		}
	}

HttpUtil 见 http://blog.csdn.net/xuyw10000/article/details/33342489

返回结果集

img_url : http://img0.aibangjuxin.com/ipic/dd3d5d81c3f14036_8.jpg
id : 693550581-432645732
desc :   滋味楼位于近郊高新开发区火炬大道近丰源集团大厦,主要经营赣南菜,农家菜,菜有点特色,口味...
rate : 4
cate : 美食;其他美食
tel : 0791-8196688
name : 滋味楼
lng : 115.9614
addr : 近郊高新开发区火炬大道(近丰源集团大厦)
cost : 43
lat : 28.69648
dist : 0.3
img_url : 
id : 1232117961-423760332
desc : 
rate : 0
cate : 美食;其他美食
tel : 
name : 渔味名厨
lng : 115.95992
addr : 青山湖区火炬二路罗万罗谢村233号
cost : 0
lat : 28.693348
dist : 0.3
img_url : 
id : 285771922-419797984
desc : 
rate : 0
cate : 小吃快餐;美食;其他小吃
tel : 18779178698
name : 广昌水饺
lng : 115.96064
addr : 近郊高新区火炬二路(纬泰超市西边斜对面)
cost : 0
lat : 28.69358
dist : 0.4
img_url : 
id : 293706066-420217555
desc :   农妇柴房,南昌市是一家知名度很高的风味餐厅。价格比较便宜、环境不错、服务态度也很好。主要...
rate : 0
cate : 美食;其他美食
tel : 
name : 农妇柴房
lng : 115.960198
addr : 高新北大道600号罗万村(近圆中源大酒店)
cost : 30
lat : 28.693144
dist : 0.4
img_url : 
id : 1805588933-943974780
desc : 
rate : 0
cate : 粤菜;茶餐厅;美食
tel : 
name : 避风塘(火炬二路店)
lng : 115.95865
addr : 青山湖区火炬二路
cost : 0
lat : 28.69278
dist : 0.4
img_url : 
id : 442301627-443221322
desc : 
rate : 0
cate : 美食;甜点饮品
tel : 
name : 金冠蛋糕(火炬二路店)
lng : 115.95915
addr : 青山湖区火炬二路
cost : 0
lat : 28.69244
dist : 0.4
img_url : 
id : 435466635-435623173
desc : 
rate : 0
cate : 美食;其他美食
tel : 
name : 鱼米之香
lng : 115.96237
addr : 青山湖区火炬二路406号(近京东大道)
cost : 0
lat : 28.69389
dist : 0.5
img_url : 
id : 559255524-424804592
desc : 
rate : 0
cate : 小吃快餐;美食;其他小吃
tel : 
name : 大食头白领快餐(艾湖店)
lng : 115.96237
addr : 青山湖区火炬二路
cost : 0
lat : 28.69389
dist : 0.5
img_url : 
id : 1227347689-423256140
desc : 
rate : 0
cate : 美食;小吃快餐;湖北菜;快餐;火锅;其他火锅
tel : 
name : 湖北风味小吃
lng : 115.96237
addr : 青山湖区火炬二路附近
cost : 0
lat : 28.69389
dist : 0.5

将结果转换成xml在微信回复多图文

public String getWeather(Map map) {
		String message=null;
		String openid = map.get("FromUserName") + "";
		String toUserName = map.get("ToUserName") + "";
		String city = map.get("city") + "";
		JSONObject obj = BaiduUtil.getWeatherInfo(city);
		String status = obj.getString("status");
		if("success".equals(status)){//查找到了
			JSONArray jarray=obj.getJSONArray("results");
			int size=jarray.size();
			if(size>0){
				JSONObject j2=jarray.getJSONObject(0);
				JSONArray jarray2=j2.getJSONArray("weather_data");
				List<Article> articleList = new ArrayList<Article>();
				// 创建图文消息
				NewsMessage newsMessage = new NewsMessage();
				newsMessage.setToUserName(openid);
				newsMessage.setFromUserName(toUserName);
				newsMessage.setCreateTime(new Date().getTime());
				newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
				newsMessage.setFuncFlag(0);
				int size2=jarray2.size();
				for (int i = 0; i < jarray2.size(); i++) {
					JSONObject j3=jarray2.getJSONObject(i);
					String date=j3.getString("date");
					String weather=j3.getString("weather");
					String wind=j3.getString("wind");
					String temperature=j3.getString("temperature");
					String title="";
					String img_url=j3.getString("dayPictureUrl");
					if(i==0){
						title=date+" "+weather+" "+wind;
					}else{
						title=date+" "+weather+" "+temperature+" "+wind;
					}
					Article article = new Article();
					article.setTitle(title);
					article.setDescription("");
					article.setPicUrl(img_url);
					article.setUrl("http://blog.csdn.net/xuyw10000");
					articleList.add(article);
				}
				newsMessage.setArticleCount(size2);
				newsMessage.setArticles(articleList);
				message = MessageUtil.newsMessageToXml(newsMessage);
			}else{
				map.put("xuywMsg", "啊噢,找不到“"+city+"”的天气。");
				message=sendTextMessage(map);
			}
		}else{//无结果
			map.put("xuywMsg", "啊噢,这个地方百度都找不到。");
			message=sendTextMessage(map);
		}
		return message;
	}

/**
	 * 发送文本信息
	 * 
	 * @param map
	 * @return
	 */
	public String sendTextMessage(Map map) {
		String openid = map.get("FromUserName") + "";
		String toUserName = map.get("ToUserName") + "";
		String xuywMsg = map.get("xuywMsg") + "";
		TextMessage textMessage = new TextMessage();
		textMessage.setToUserName(openid);
		textMessage.setFromUserName(toUserName);
		textMessage.setCreateTime(new Date().getTime());
		textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
		textMessage.setFuncFlag(0);
		textMessage.setContent(xuywMsg);
		return MessageUtil.textMessageToXml(textMessage);
	}



执行返回xml 

<xml>
  <ToUserName><![CDATA[sadsdaewwaewea]]></ToUserName>
  <FromUserName><![CDATA[bbbbb]]></FromUserName>
  <CreateTime><![CDATA[1403495107127]]></CreateTime>
  <MsgType><![CDATA[news]]></MsgType>
  <FuncFlag><![CDATA[0]]></FuncFlag>
  <ArticleCount><![CDATA[9]]></ArticleCount>
  <Articles>
    <item>
      <Title><![CDATA[距离0.3千米]]></Title>
      <Description><![CDATA[近郊高新开发区火炬大道(近丰源集团大厦)]]></Description>
      <PicUrl><![CDATA[http://img0.aibangjuxin.com/ipic/dd3d5d81c3f14036_8.jpg]]></PicUrl>
      <Url><![CDATA[http://api.map.baidu.com/marker?location=28.69648,115.9614&title=滋味楼&content=近郊高新开发区火炬大道(近丰源集团大厦)&output=html]]></Url>
    </item>
    <item>
      <Title><![CDATA[距离0.3千米]]></Title>
      <Description><![CDATA[青山湖区火炬二路罗万罗谢村233号]]></Description>
      <PicUrl><![CDATA[http://xywwx.sinaapp.com/resource/img/nopic.jpg]]></PicUrl>
      <Url><![CDATA[http://api.map.baidu.com/marker?location=28.693348,115.95992&title=渔味名厨&content=青山湖区火炬二路罗万罗谢村233号&output=html]]></Url>
    </item>
    <item>
      <Title><![CDATA[距离0.4千米]]></Title>
      <Description><![CDATA[近郊高新区火炬二路(纬泰超市西边斜对面)]]></Description>
      <PicUrl><![CDATA[http://xywwx.sinaapp.com/resource/img/nopic.jpg]]></PicUrl>
      <Url><![CDATA[http://api.map.baidu.com/marker?location=28.69358,115.96064&title=广昌水饺&content=近郊高新区火炬二路(纬泰超市西边斜对面)&output=html]]></Url>
    </item>
    <item>
      <Title><![CDATA[距离0.4千米]]></Title>
      <Description><![CDATA[高新北大道600号罗万村(近圆中源大酒店)]]></Description>
      <PicUrl><![CDATA[http://xywwx.sinaapp.com/resource/img/nopic.jpg]]></PicUrl>
      <Url><![CDATA[http://api.map.baidu.com/marker?location=28.693144,115.960198&title=农妇柴房&content=高新北大道600号罗万村(近圆中源大酒店)&output=html]]></Url>
    </item>
    <item>
      <Title><![CDATA[距离0.4千米]]></Title>
      <Description><![CDATA[青山湖区火炬二路]]></Description>
      <PicUrl><![CDATA[http://xywwx.sinaapp.com/resource/img/nopic.jpg]]></PicUrl>
      <Url><![CDATA[http://api.map.baidu.com/marker?location=28.69278,115.95865&title=避风塘(火炬二路店)&content=青山湖区火炬二路&output=html]]></Url>
    </item>
    <item>
      <Title><![CDATA[距离0.4千米]]></Title>
      <Description><![CDATA[青山湖区火炬二路]]></Description>
      <PicUrl><![CDATA[http://xywwx.sinaapp.com/resource/img/nopic.jpg]]></PicUrl>
      <Url><![CDATA[http://api.map.baidu.com/marker?location=28.69244,115.95915&title=金冠蛋糕(火炬二路店)&content=青山湖区火炬二路&output=html]]></Url>
    </item>
    <item>
      <Title><![CDATA[距离0.5千米]]></Title>
      <Description><![CDATA[青山湖区火炬二路406号(近京东大道)]]></Description>
      <PicUrl><![CDATA[http://xywwx.sinaapp.com/resource/img/nopic.jpg]]></PicUrl>
      <Url><![CDATA[http://api.map.baidu.com/marker?location=28.69389,115.96237&title=鱼米之香&content=青山湖区火炬二路406号(近京东大道)&output=html]]></Url>
    </item>
    <item>
      <Title><![CDATA[距离0.5千米]]></Title>
      <Description><![CDATA[青山湖区火炬二路]]></Description>
      <PicUrl><![CDATA[http://xywwx.sinaapp.com/resource/img/nopic.jpg]]></PicUrl>
      <Url><![CDATA[http://api.map.baidu.com/marker?location=28.69389,115.96237&title=大食头白领快餐(艾湖店)&content=青山湖区火炬二路&output=html]]></Url>
    </item>
    <item>
      <Title><![CDATA[距离0.5千米]]></Title>
      <Description><![CDATA[青山湖区火炬二路附近]]></Description>
      <PicUrl><![CDATA[http://xywwx.sinaapp.com/resource/img/nopic.jpg]]></PicUrl>
      <Url><![CDATA[http://api.map.baidu.com/marker?location=28.69389,115.96237&title=湖北风味小吃&content=青山湖区火炬二路附近&output=html]]></Url>
    </item>
  </Articles>
</xml>

将项目部署sae 请求

bubuko.com,布布扣

bubuko.com,布布扣

微信爱帮周边查询,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!