偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

微信公眾平臺(tái)開發(fā)(三)位置信息的識(shí)別

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

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

1.微信地理位置信息

2.路名、標(biāo)志性建筑或是商場(chǎng)名稱

1.微信地理位置消息

認(rèn)識(shí)一下,微信地理位置消息,包含一些什么信息

  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>  

包含的主要信息有經(jīng)度緯度和Label的位置??梢愿鶕?jù)label中描述的位置信息,提供給用戶對(duì)應(yīng)的服務(wù)。也可根據(jù)用戶的經(jīng)度緯度信息,提供你最近的產(chǎn)品或是有地域性的產(chǎn)品。

[[70582]]

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

  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; 

輸出的結(jié)果

  1. <PlaceSearchResponse> 
  2.     <status>OK</status> 
  3.     <results> 
  4.         <result> 
  5.             <name>中國(guó)工商銀行東長(zhǎng)安街支行</name> 
  6.             <location> 
  7.                 <lat>39.915891</lat> 
  8.                 <lng>116.41867</lng> 
  9.             </location> 
  10.             <address>東城區(qū)東長(zhǎng)安街1號(hào)東方廣場(chǎng)西三辦公樓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> 

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

  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.     } 

別走開,下頁(yè)更勁爆~

#p#

2.路名、標(biāo)志性建筑或是商場(chǎng)名稱

對(duì)路名、標(biāo)志性建筑等信息,方法還是通過(guò)第三方地圖信息,確定輸入的位置信息的經(jīng)度緯度。

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

  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.     } 

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

3.源代碼

本文的代碼較長(zhǎng),提供源代碼下載

WeChatDemo下載

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

閱讀第二篇:微信公眾平臺(tái)開發(fā)(二)簡(jiǎn)單的聊天機(jī)器人

轉(zhuǎn)載自http://www.qiyadeng.com/

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

責(zé)任編輯:閆佳明 來(lái)源: cnblogs
相關(guān)推薦

2013-04-15 16:56:48

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

2013-04-10 18:45:52

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

2013-04-09 17:23:57

微信微信公眾平臺(tái)歡迎信息

2013-04-10 18:07:08

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

2013-04-10 18:19:40

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

2013-04-10 16:15:40

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

2013-05-24 09:35:46

Java實(shí)現(xiàn)

2013-04-11 10:50:07

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

2013-04-09 23:38:02

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

2013-04-10 18:29:09

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

2013-04-10 18:24:48

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

2013-04-10 17:59:50

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

2014-11-20 09:38:40

C#

2013-04-08 15:13:39

微信公眾平臺(tái)

2013-04-10 16:51:56

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

2013-04-03 09:08:45

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

2013-11-01 09:21:47

微信微信公眾平臺(tái)微信公眾賬號(hào)

2013-03-25 16:35:04

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

2013-04-15 17:02:33

2013-04-10 17:52:15

微信公眾平臺(tái)接口開發(fā)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)