Posted on 2013-03-26 22:45
qiyadeng 閱讀(1773)
評(píng)論(0) 編輯 收藏
到了一個(gè)較陌生的環(huán)境,經(jīng)常會(huì)在周邊找一些基礎(chǔ)設(shè)施,比如銀行,商場(chǎng),餐廳等(還有一種更急切的是找?guī)Mㄟ^百度提供的地圖API,可以在你的應(yīng)用中簡(jiǎn)單做到,詳情可閱讀
Place API。我們以查找周邊銀行作為示例,需確定的參數(shù)至少有三個(gè),要查找的位置的經(jīng)度和緯度,需要查找的內(nèi)容的類型或是關(guān)鍵字。
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);//位置xml
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;
}
Junit測(cè)試
@Test
public void testGetBaiduPlace() throws Exception{
BaiduMapService bms = new BaiduMapService();
String response = bms.getPalace("銀行", "39.915", "116.404");
List<BaiduPlaceResponse> list = BaiduPlaceResponse.getBaiduPlace(response);
for(BaiduPlaceResponse res:list){
System.out.println(res.toString());
}
}
輸出內(nèi)容(省略部分內(nèi)容)
<?xml version="1.0" encoding="utf-8" ?>
<PlaceSearchResponse>
<status>OK</status>
<results>
<result>
<name>中國(guó)工商銀行東長(zhǎng)安街支行</name>
<location>
<lat>39.915891</lat>
<lng>116.41867</lng>
</location>
<address>東城區(qū)東長(zhǎng)安街1號(hào)東方廣場(chǎng)西三辦公樓1樓</address>
<uid>a025683c73033c35a21de987</uid>
<detail_url>http://api.map.baidu.com/place/detail?uid=a025683c73033c35a21de987&amp;output=html&amp;source=placeapi</detail_url>
<tag>銀行,王府井/東單</tag>
</result>
</results>
</PlaceSearchResponse>
BaiduPlaceResponse [name=中國(guó)工商銀行東長(zhǎng)安街支行, telephone=null, address=東城區(qū)東長(zhǎng)安街1號(hào)東方廣場(chǎng)西三辦公樓1樓, lat=39.915891, lng=116.41867, tag=null, detailUrl=http://api.map.baidu.com/place/detail?uid=a025683c73033c35a21de987&output=html&source=placeapi]