到了一个较陌生的环境,经常会在周边找一些基础设施,比如银行,商场,餐厅等(还有一种更急切的是找厕所)。通过百度提供的地图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]