位置识别这是实际应用经常应用的消息,特别是很多商家,通过了解用户位置,给用户提供特别的产品或是商场的推荐。其中用户可能发送两种类型的消息:
创新互联是一家专注于做网站、成都做网站与策划设计,柞水网站建设哪家好?创新互联做网站,专注于网站建设十余年,网设计领域的专业建站公司;建站业务涵盖:柞水等地区。柞水做网站价格咨询:18980820575
1.微信地理位置信息
2.路名、标志性建筑或是商场名称
认识一下,微信地理位置消息,包含一些什么信息
1351776360 23.134521 113.358803 20 1234567890123456
包含的主要信息有经度纬度和Label的位置。可以根据label中描述的位置信息,提供给用户对应的服务。也可根据用户的经度纬度信息,提供你最近的产品或是有地域性的产品。
首先根据微信的地理位置信息,定义WeChatLocationMessage类,并能把Xml转换为WeChatLocationMessage对象
- public class WeChatLocationMessage {
- private String toUserName;
- private String fromUserName;
- private String createTime;
- private String msgType;
- private String locationx;
- private String localtiony;
- private String scale;
- private String label;
- private String msgId;
- public static WeChatLocationMessage getWeChatLocationMessage(String xml){
- XStream xstream = new XStream(new DomDriver());
- WeChatLocationMessage message = null;
- xstream.alias("xml", WeChatLocationMessage.class);
- xstream.aliasField("ToUserName", WeChatLocationMessage.class, "toUserName");
- xstream.aliasField("FromUserName", WeChatLocationMessage.class, "fromUserName");
- xstream.aliasField("CreateTime", WeChatLocationMessage.class, "createTime");
- xstream.aliasField("MsgType", WeChatLocationMessage.class, "msgType");
- xstream.aliasField("Location_X", WeChatLocationMessage.class, "locationx");
- xstream.aliasField("Location_Y", WeChatLocationMessage.class, "localtiony");
- xstream.aliasField("Scale", WeChatLocationMessage.class, "scale");
- xstream.aliasField("Label", WeChatLocationMessage.class, "label");
- xstream.aliasField("MsgId", WeChatLocationMessage.class, "msgId");
- message = (WeChatLocationMessage)xstream.fromXML(xml);
- return message;
- }
- //getter and setter
- }
本文利用百度的地图API,查找最近的银行做为示例。
- public String getPalace(String query,String lat,String lng) throws ClientProtocolException, IOException{
- HttpClient httpClient = new DefaultHttpClient();
- String url = palceRequestUrl(query,lat,lng);
- logger.log(Level.INFO, url);
- HttpGet httpget = new HttpGet(url);
- ResponseHandler
responseHandler = new BasicResponseHandler(); - String responseBody = httpClient.execute(httpget, responseHandler);
- logger.log(Level.INFO,"baidu response:"+responseBody);
- return responseBody;
- }
- public String palceRequestUrl(String query,String lat,String lng) throws UnsupportedEncodingException {
- String url = WeChatConstant.BASEURL + "place/search?query=" + URLEncoder.encode(query,"UTF-8") + "&key="
- + WeChatConstant.MAPKEY +"&location="+lat+","+lng +"&radius=2000"+"&output=" + WeChatConstant.OUTPUTFORMAT;
- return url;
- }
输出的结果
OK 中国工商银行东长安街支行 39.915891 116.41867 - 东城区东长安街1号东方广场西三办公楼1楼
a025683c73033c35a21de987 http://api.map.baidu.com/place/detail?uid=a025683c73033c35a21de987&output=html&source=placeapi 银行,王府井/东单
接下来,把百度地图反映出来的最近位置信息,以图文消息的格式展示给微信用户
- public static String getWeChatReplyNewsMessageByBaiduPlace(List
placeList, double lat, double lng,String userName, int size){ - WeChatReplyNewsMessage newsMessage = new WeChatReplyNewsMessage();
- List
- items = new ArrayList
- ();
- StringBuffer strBuf = new StringBuffer();
- logger.log(Level.INFO,"placeList count="+placeList.size());
- newsMessage.setItems(items);
- if(placeList.size()>size){
- newsMessage.setArticleCount(size);
- }
- else{
- newsMessage.setArticleCount(placeList.size());
- }
- logger.log(Level.INFO,"article count="+newsMessage.getArticleCount());
- newsMessage.setCreateTime(new Date().getTime()+"");
- newsMessage.setMsgType("news");
- newsMessage.setFuncFlag("0");
- newsMessage.setToUserName(userName);
- newsMessage.setFromUserName(WeChatConstant.FROMUSERNAME);
- for(int i = 0;i
- BaiduPlaceResponse place = placeList.get(i);
- Double distance = GeoUtil.DistanceOfTwoPoints(Double.valueOf(place.getLng()), Double.valueOf(place.getLat()), lng, lat, GaussSphere.Beijing54);
- Item item = new Item();
- item.setTitle(place.getName()+"["+distance+"米]"+"\n"+place.getAddress()+"\n"+place.getTelephone());
- item.setPicUrl("");
- item.setUrl(place.getDetailUrl());
- item.setDescription("");
- items.add(item);
- }
- logger.log(Level.INFO,"newMessage="+newsMessage.toString());
- strBuf = strBuf.append(getWeChatNewsMessage(newsMessage));
- return strBuf.toString();
- }
- public static String getWeChatNewsMessage(WeChatReplyNewsMessage newsMessage){
- XStream xstream = new XStream(new DomDriver());
- xstream.alias("xml", WeChatReplyNewsMessage.class);
- xstream.aliasField("ToUserName", WeChatReplyNewsMessage.class, "toUserName");
- xstream.aliasField("FromUserName", WeChatReplyNewsMessage.class, "fromUserName");
- xstream.aliasField("CreateTime", WeChatReplyNewsMessage.class, "createTime");
- xstream.aliasField("MsgType", WeChatReplyNewsMessage.class, "msgType");
- xstream.aliasField("ArticleCount", WeChatReplyNewsMessage.class, "articleCount");
- xstream.aliasField("Content", WeChatReplyNewsMessage.class, "content");
- xstream.aliasField("FuncFlag", WeChatReplyNewsMessage.class, "funcFlag");
- xstream.aliasField("Articles", WeChatReplyNewsMessage.class, "items");
- xstream.alias("item", Item.class);
- xstream.aliasField("Title", Item.class, "title");
- xstream.aliasField("Description", Item.class, "description");
- xstream.aliasField("PicUrl", Item.class, "picUrl");
- xstream.aliasField("Url", Item.class, "url");
- return xstream.toXML(newsMessage);
- }
#p#
对路名、标志性建筑等信息,方法还是通过第三方地图信息,确定输入的位置信息的经度纬度。
本文使用百度地图API,确定所查找的位置的经度和纬度。
- public String getGeoCode(String query) throws ClientProtocolException, IOException{
- HttpClient httpClient = new DefaultHttpClient();
- String url = geoCodeRequestUrl(query);
- logger.log(Level.INFO, url);
- HttpGet httpget = new HttpGet(url);
- ResponseHandler
responseHandler = new BasicResponseHandler(); - String responseBody = httpClient.execute(httpget, responseHandler);
- logger.log(Level.INFO,"baidu response:"+responseBody);
- return responseBody;
- }
- public String geoCodeRequestUrl(String query) throws UnsupportedEncodingException{
- String url = WeChatConstant.BASEURL + "geocoder?address=" + URLEncoder.encode(query,"UTF-8") + "&key="
- + WeChatConstant.MAPKEY + "&output=" + WeChatConstant.OUTPUTFORMAT;
- return url;
- }
确定了经度和纬度,问题就变成和第1种消息类型一致了,根据经度纬度去做相应处理。
本文的代码较长,提供源代码下载
转载自http://www.qiyadeng.com/
本文链接地址: 微信公众平台开发(三)–位置信息的识别
网站栏目:微信公众平台开发(三)位置信息的识别
文章出自:http://www.36103.cn/qtweb/news24/1774.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联