新聞中心
位置識別這是實際應(yīng)用經(jīng)常應(yīng)用的消息,特別是很多商家,通過了解用戶位置,給用戶提供特別的產(chǎn)品或是商場的推薦。其中用戶可能發(fā)送兩種類型的消息:

創(chuàng)新互聯(lián)是一家專注于做網(wǎng)站、成都做網(wǎng)站與策劃設(shè)計,柞水網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:柞水等地區(qū)。柞水做網(wǎng)站價格咨詢:18980820575
1.微信地理位置信息
2.路名、標(biāo)志性建筑或是商場名稱
1.微信地理位置消息
認(rèn)識一下,微信地理位置消息,包含一些什么信息
1351776360 23.134521 113.358803 20 1234567890123456
包含的主要信息有經(jīng)度緯度和Label的位置??梢愿鶕?jù)label中描述的位置信息,提供給用戶對應(yīng)的服務(wù)。也可根據(jù)用戶的經(jīng)度緯度信息,提供你最近的產(chǎn)品或是有地域性的產(chǎn)品。
首先根據(jù)微信的地理位置信息,定義WeChatLocationMessage類,并能把Xml轉(zhuǎn)換為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;
- }
輸出的結(jié)果
OK 中國工商銀行東長安街支行 39.915891 116.41867 - 東城區(qū)東長安街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#
2.路名、標(biāo)志性建筑或是商場名稱
對路名、標(biāo)志性建筑等信息,方法還是通過第三方地圖信息,確定輸入的位置信息的經(jīng)度緯度。
本文使用百度地圖API,確定所查找的位置的經(jīng)度和緯度。
- 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;
- }
確定了經(jīng)度和緯度,問題就變成和第1種消息類型一致了,根據(jù)經(jīng)度緯度去做相應(yīng)處理。
3.源代碼
本文的代碼較長,提供源代碼下載
WeChatDemo下載
閱讀第一篇:微信公眾平臺開發(fā)(一)平臺綜述
閱讀第二篇:微信公眾平臺開發(fā)(二)簡單的聊天機器人
轉(zhuǎn)載自http://www.qiyadeng.com/
本文鏈接地址: 微信公眾平臺開發(fā)(三)–位置信息的識別
新聞名稱:微信公眾平臺開發(fā)(三)位置信息的識別
文章鏈接:http://www.dlmjj.cn/article/dpoogsg.html


咨詢
建站咨詢
