自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

微信公眾平臺開發(fā)(三)位置信息的識別

移動開發(fā) Android
微信公眾平臺是騰訊公司在微信的基礎上新增的功能模塊,通過這一平臺,個人和企業(yè)都可以打造一個微信的公眾號,并實現(xiàn)和特定群體的文字、圖片、語音的全方位溝通、互動。

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

1.微信地理位置信息

2.路名、標志性建筑或是商場名稱

1.微信地理位置消息

認識一下,微信地理位置消息,包含一些什么信息

  1. <xml> 
  2. <ToUserName><![CDATA[toUser]]></ToUserName> 
  3. <FromUserName><![CDATA[fromUser]]></FromUserName> 
  4. <CreateTime>1351776360</CreateTime> 
  5. <MsgType><![CDATA[location]]></MsgType> 
  6. <Location_X>23.134521</Location_X> 
  7. <Location_Y>113.358803</Location_Y> 
  8. <Scale>20</Scale> 
  9. <Label><![CDATA[位置信息]]></Label> 
  10. <MsgId>1234567890123456</MsgId> 
  11. </xml>  

包含的主要信息有經度緯度和Label的位置。可以根據(jù)label中描述的位置信息,提供給用戶對應的服務。也可根據(jù)用戶的經度緯度信息,提供你最近的產品或是有地域性的產品。

[[70582]]

首先根據(jù)微信的地理位置信息,定義WeChatLocationMessage類,并能把Xml轉換為WeChatLocationMessage對象

  1. public class WeChatLocationMessage { 
  2.     private String toUserName; 
  3.     private String fromUserName; 
  4.     private String createTime; 
  5.     private String msgType; 
  6.     private String locationx; 
  7.     private String localtiony; 
  8.     private String scale; 
  9.     private String label; 
  10.     private String msgId; 
  11.     public static WeChatLocationMessage getWeChatLocationMessage(String xml){ 
  12.         XStream xstream = new XStream(new DomDriver()); 
  13.         WeChatLocationMessage  message = null
  14.         xstream.alias("xml", WeChatLocationMessage.class); 
  15.         xstream.aliasField("ToUserName", WeChatLocationMessage.class"toUserName"); 
  16.         xstream.aliasField("FromUserName", WeChatLocationMessage.class"fromUserName"); 
  17.         xstream.aliasField("CreateTime", WeChatLocationMessage.class"createTime"); 
  18.         xstream.aliasField("MsgType", WeChatLocationMessage.class"msgType"); 
  19.         xstream.aliasField("Location_X", WeChatLocationMessage.class"locationx"); 
  20.         xstream.aliasField("Location_Y", WeChatLocationMessage.class"localtiony"); 
  21.         xstream.aliasField("Scale", WeChatLocationMessage.class"scale"); 
  22.         xstream.aliasField("Label", WeChatLocationMessage.class"label"); 
  23.         xstream.aliasField("MsgId", WeChatLocationMessage.class"msgId"); 
  24.         message = (WeChatLocationMessage)xstream.fromXML(xml); 
  25.         return message; 
  26.     } 
  27. //getter and setter 

本文利用百度的地圖API,查找最近的銀行做為示例。

  1. public String getPalace(String query,String lat,String lng) throws ClientProtocolException, IOException{ 
  2.     HttpClient httpClient = new DefaultHttpClient(); 
  3.     String url = palceRequestUrl(query,lat,lng); 
  4.     logger.log(Level.INFO, url); 
  5.     HttpGet httpget = new HttpGet(url); 
  6.     ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
  7.     String responseBody = httpClient.execute(httpget, responseHandler); 
  8.     logger.log(Level.INFO,"baidu response:"+responseBody); 
  9.     return responseBody; 
  10.  
  11. public String palceRequestUrl(String query,String lat,String lng) throws UnsupportedEncodingException { 
  12.     String url = WeChatConstant.BASEURL + "place/search?query=" + URLEncoder.encode(query,"UTF-8") + "&key=" 
  13.             + WeChatConstant.MAPKEY +"&location="+lat+","+lng +"&radius=2000"+"&output=" + WeChatConstant.OUTPUTFORMAT; 
  14.     return url; 

輸出的結果

  1. <PlaceSearchResponse> 
  2.     <status>OK</status> 
  3.     <results> 
  4.         <result> 
  5.             <name>中國工商銀行東長安街支行</name> 
  6.             <location> 
  7.                 <lat>39.915891</lat> 
  8.                 <lng>116.41867</lng> 
  9.             </location> 
  10.             <address>東城區(qū)東長安街1號東方廣場西三辦公樓1樓</address> 
  11.             <uid>a025683c73033c35a21de987</uid> 
  12.             <detail_url>http://api.map.baidu.com/place/detail?uid=a025683c73033c35a21de987&amp;amp;output=html&amp;amp;source=placeapi 
  13.             </detail_url> 
  14.             <tag>銀行,王府井/東單</tag> 
  15.         </result> 
  16.       </results> 
  17. </PlaceSearchResponse> 

接下來,把百度地圖反映出來的最近位置信息,以圖文消息的格式展示給微信用戶

  1.     public static String getWeChatReplyNewsMessageByBaiduPlace(List<BaiduPlaceResponse> placeList, double lat, double lng,String userName, int size){ 
  2.         WeChatReplyNewsMessage newsMessage = new WeChatReplyNewsMessage(); 
  3.         List<Item> items = new ArrayList<Item>(); 
  4.         StringBuffer strBuf = new StringBuffer(); 
  5.         logger.log(Level.INFO,"placeList count="+placeList.size()); 
  6.         newsMessage.setItems(items); 
  7.         if(placeList.size()>size){ 
  8.             newsMessage.setArticleCount(size); 
  9.         } 
  10.         else
  11.             newsMessage.setArticleCount(placeList.size()); 
  12.         } 
  13.         logger.log(Level.INFO,"article count="+newsMessage.getArticleCount()); 
  14.         newsMessage.setCreateTime(new Date().getTime()+""); 
  15.         newsMessage.setMsgType("news"); 
  16.         newsMessage.setFuncFlag("0"); 
  17.         newsMessage.setToUserName(userName); 
  18.         newsMessage.setFromUserName(WeChatConstant.FROMUSERNAME); 
  19.         for(int i = 0;i <newsMessage.getArticleCount();i++){ 
  20.             BaiduPlaceResponse place = placeList.get(i); 
  21.             Double distance = GeoUtil.DistanceOfTwoPoints(Double.valueOf(place.getLng()), Double.valueOf(place.getLat()), lng, lat, GaussSphere.Beijing54); 
  22.             Item item = new Item(); 
  23.             item.setTitle(place.getName()+"["+distance+"米]"+"\n"+place.getAddress()+"\n"+place.getTelephone()); 
  24.             item.setPicUrl(""); 
  25.             item.setUrl(place.getDetailUrl()); 
  26.             item.setDescription(""); 
  27.             items.add(item); 
  28.         } 
  29. logger.log(Level.INFO,"newMessage="+newsMessage.toString()); 
  30.         strBuf = strBuf.append(getWeChatNewsMessage(newsMessage)); 
  31.         return strBuf.toString(); 
  32.     } 
  33.     public static String getWeChatNewsMessage(WeChatReplyNewsMessage newsMessage){ 
  34.         XStream xstream = new XStream(new DomDriver()); 
  35.         xstream.alias("xml", WeChatReplyNewsMessage.class); 
  36.         xstream.aliasField("ToUserName", WeChatReplyNewsMessage.class"toUserName"); 
  37.         xstream.aliasField("FromUserName", WeChatReplyNewsMessage.class"fromUserName"); 
  38.         xstream.aliasField("CreateTime", WeChatReplyNewsMessage.class"createTime"); 
  39.         xstream.aliasField("MsgType", WeChatReplyNewsMessage.class"msgType"); 
  40.         xstream.aliasField("ArticleCount", WeChatReplyNewsMessage.class"articleCount"); 
  41.         xstream.aliasField("Content", WeChatReplyNewsMessage.class"content"); 
  42.         xstream.aliasField("FuncFlag", WeChatReplyNewsMessage.class"funcFlag"); 
  43.         xstream.aliasField("Articles", WeChatReplyNewsMessage.class"items"); 
  44.         xstream.alias("item", Item.class); 
  45.         xstream.aliasField("Title", Item.class"title"); 
  46.         xstream.aliasField("Description", Item.class"description"); 
  47.         xstream.aliasField("PicUrl", Item.class"picUrl"); 
  48.         xstream.aliasField("Url", Item.class"url"); 
  49.         return xstream.toXML(newsMessage); 
  50.     } 

別走開,下頁更勁爆~

#p#

2.路名、標志性建筑或是商場名稱

對路名、標志性建筑等信息,方法還是通過第三方地圖信息,確定輸入的位置信息的經度緯度。

本文使用百度地圖API,確定所查找的位置的經度和緯度。

  1. public String getGeoCode(String query) throws ClientProtocolException, IOException{ 
  2.         HttpClient httpClient = new DefaultHttpClient(); 
  3.         String url = geoCodeRequestUrl(query); 
  4.         logger.log(Level.INFO, url); 
  5.         HttpGet httpget = new HttpGet(url); 
  6.         ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
  7.         String responseBody = httpClient.execute(httpget, responseHandler); 
  8.         logger.log(Level.INFO,"baidu response:"+responseBody); 
  9.         return responseBody; 
  10.     } 
  11.     public String geoCodeRequestUrl(String query) throws UnsupportedEncodingException{ 
  12.         String url = WeChatConstant.BASEURL + "geocoder?address=" + URLEncoder.encode(query,"UTF-8") + "&key=" 
  13.                 + WeChatConstant.MAPKEY + "&output=" + WeChatConstant.OUTPUTFORMAT; 
  14.         return url; 
  15.     } 

確定了經度和緯度,問題就變成和第1種消息類型一致了,根據(jù)經度緯度去做相應處理。

3.源代碼

本文的代碼較長,提供源代碼下載

WeChatDemo下載

閱讀第一篇:微信公眾平臺開發(fā)(一)平臺綜述

閱讀第二篇:微信公眾平臺開發(fā)(二)簡單的聊天機器人

轉載自http://www.qiyadeng.com/

本文鏈接地址: 微信公眾平臺開發(fā)(三)–位置信息的識別

責任編輯:閆佳明 來源: cnblogs
相關推薦

2013-04-15 16:56:48

微信公眾平臺Android開發(fā)

2013-04-10 18:45:52

微信公眾平臺接口開發(fā)

2013-04-09 17:23:57

微信微信公眾平臺歡迎信息

2013-04-10 18:07:08

微信公眾平臺接口開發(fā)

2013-04-10 16:15:40

微信公眾平臺接口開發(fā)

2013-04-10 18:19:40

微信公眾平臺接口開發(fā)

2013-05-24 09:35:46

Java實現(xiàn)

2013-04-11 10:50:07

微信公眾平臺接口開發(fā)

2013-04-09 23:38:02

微信公眾平臺開發(fā)者

2013-04-10 18:24:48

微信公眾平臺接口開發(fā)

2013-04-10 18:29:09

微信公眾平臺接口開發(fā)

2014-11-20 09:38:40

C#

2013-04-10 17:59:50

微信公眾平臺接口開發(fā)

2013-04-08 15:13:39

微信公眾平臺

2013-04-03 09:08:45

陶瑾微信公眾平臺微信開發(fā)者

2013-04-10 16:51:56

微信公眾平臺接口開發(fā)

2013-11-01 09:21:47

微信微信公眾平臺微信公眾賬號

2013-03-25 16:35:04

微信微信公眾平臺開發(fā)者

2013-04-10 17:52:15

微信公眾平臺接口開發(fā)

2013-04-15 17:02:33

點贊
收藏

51CTO技術棧公眾號