Posted on 2013-03-26 22:45
qiyadeng 閱讀(1773)
評論(0) 編輯 收藏
到了一個較陌生的環境,經常會在周邊找一些基礎設施,比如銀行,商場,餐廳等(還有一種更急切的是找廁所)。通過百度提供的地圖API,可以在你的應用中簡單做到,詳情可閱讀
Place 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);//位置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測試
@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());
}
}
輸出內容(省略部分內容)
<?xml version="1.0" encoding="utf-8" ?>
<PlaceSearchResponse>
<status>OK</status>
<results>
<result>
<name>中國工商銀行東長安街支行</name>
<location>
<lat>39.915891</lat>
<lng>116.41867</lng>
</location>
<address>東城區東長安街1號東方廣場西三辦公樓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=中國工商銀行東長安街支行, telephone=null, address=東城區東長安街1號東方廣場西三辦公樓1樓, lat=39.915891, lng=116.41867, tag=null, detailUrl=http://api.map.baidu.com/place/detail?uid=a025683c73033c35a21de987&output=html&source=placeapi]