<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    qiyadeng

    專注于Java示例及教程
    posts - 84, comments - 152, trackbacks - 0, articles - 34

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

    1.微信地理位置信息

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

    1.微信地理位置消息

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


    <xml>
    <ToUserName><![CDATA[toUser]]></ToUserName>
    <FromUserName><![CDATA[fromUser]]></FromUserName>
    <CreateTime>1351776360</CreateTime>
    <MsgType><![CDATA[location]]></MsgType>
    <Location_X>23.134521</Location_X>
    <Location_Y>113.358803</Location_Y>
    <Scale>20</Scale>
    <Label><![CDATA[位置信息]]></Label>
    <MsgId>1234567890123456</MsgId>
    </xml>

    包含的主要信息有經(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
    <String> 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é)果
    <PlaceSearchResponse>
        
    <status>OK</status>
        
    <results>
            
    <result>
                
    <name>中國工商銀行東長安街支行</name>
                
    <location>
                    
    <lat>39.915891</lat>
                    
    <lng>116.41867</lng>
                
    </location>
                
    <address>東城區(qū)東長安街1號東方廣場西三辦公樓1樓</address>
                
    <uid>a025683c73033c35a21de987</uid>
                
    <detail_url>http://api.map.baidu.com/place/detail?uid=a025683c73033c35a21de987&amp;amp;output=html&amp;amp;source=placeapi
                </detail_url>
                
    <tag>銀行,王府井/東單</tag>

            
    </result>
          
    </results>
    </PlaceSearchResponse>

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

        public static String getWeChatReplyNewsMessageByBaiduPlace(List<BaiduPlaceResponse> placeList, double lat, double lng,String userName, int size){
            WeChatReplyNewsMessage newsMessage 
    = new WeChatReplyNewsMessage();
            List
    <Item> items = new ArrayList<Item>();
            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 <newsMessage.getArticleCount();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);
        }

    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
    <String> 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下載

    原創(chuàng)文章,轉(zhuǎn)載請注明: 轉(zhuǎn)載自http://www.qiyadeng.com/

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











    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 黄色毛片免费观看| 亚洲?V乱码久久精品蜜桃| 成人国产网站v片免费观看| 亚洲精品成人av在线| 免费在线观看a级毛片| 成人免费午夜无码视频| 韩日电影在线播放免费版| 国产亚洲人成在线播放| 亚洲理论片在线观看| 国产亚洲精品国产| 日产国产精品亚洲系列| 妞干网免费视频在线观看| 99re免费视频| 丝瓜app免费下载网址进入ios| 黑人粗长大战亚洲女2021国产精品成人免费视频| 精品日韩亚洲AV无码| 亚洲精品无码成人片久久| 亚洲国产精品尤物yw在线 | 亚洲日韩国产精品乱| 香蕉视频在线观看免费国产婷婷| 足恋玩丝袜脚视频免费网站| 国产日韩AV免费无码一区二区| 一级美国片免费看| 老司机午夜性生免费福利| 在线观看亚洲精品专区| 亚洲精品宾馆在线精品酒店| 91在线亚洲综合在线| 国产成人亚洲精品| 2020天堂在线亚洲精品专区| 亚洲一区二区三区免费观看| 91亚洲自偷在线观看国产馆| 亚洲国产精品张柏芝在线观看 | 91精品国产免费网站| 在线观看免费播放av片| 91免费福利视频| 三年在线观看免费观看完整版中文| 本道天堂成在人线av无码免费| 人成免费在线视频| 中文字幕a∨在线乱码免费看| 国产一二三四区乱码免费| 免费无码又爽又刺激一高潮|