/**
* 百度placeapi通过关键字,经纬度获取周边
* @param modelName
* @param latitude
* @param longitude
* @return <placeAPI>list
*/
private List placeList(String modelName,float latitude,float longitude) {
List list=new ArrayList();
String telephone;
try{
String palceUrl="http://api.map.baidu.com/place/v2/search";
String queryString="ak=tEtTWYjFFmUxyqoegemuktaA&query="+modelName+"&location="+latitude+","+longitude+"&radius=1000&output=json&scope=2&page_size=20";
HttpClientParams httpConnectionParams = new HttpClientParams();
httpConnectionParams.setConnectionManagerTimeout(10000);
HttpClient client=new HttpClient(httpConnectionParams);
HttpMethod method=new GetMethod(palceUrl);
String encodeQuery;
encodeQuery = URIUtil.encodeQuery(queryString,"UTF-8");
method.setQueryString(encodeQuery);
client.executeMethod(method);
BufferedReader reader=null;
reader=new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(),"UTF-8"));
String line=null;
StringBuffer respons=new StringBuffer();
while((line=reader.readLine())!=null){
respons.append(line).append(System.getProperty("line.separator"));
}
reader.close();
method.releaseConnection();
String sTotalString=respons.toString();
JSONObject json;
json=JSONObject.fromString(sTotalString);
JSONArray results = json.getJSONArray("results");
for(int i=0;i<results.length();i++){
Location_place location=new Location_place();
PlaceAPI placeAPI=new PlaceAPI();
Detail_info detail_info=new Detail_info();
JSONObject result=results.getJSONObject(i);
placeAPI.setName(result.getString("name"));
placeAPI.setAddress(result.getString("address"));
try{
telephone=result.getString("telephone");
}
catch(Exception e){
telephone="";
}
placeAPI.setTelephone(telephone);
JSONObject jsonlocation= (JSONObject) result.get("location");
float lat=Float.parseFloat(jsonlocation.getString("lat"));
float lng=Float.parseFloat(jsonlocation.getString("lng"));
location.setLat(lat);
location.setLng(lng);
placeAPI.setLocation(location);
JSONObject jsondetail_info=(JSONObject) result.get("detail_info");
int distance=Integer.parseInt(jsondetail_info.getString("distance"));
detail_info.setDistance(distance);
placeAPI.setDetail_info(detail_info);
list.add(placeAPI) ;
}
}
catch(Exception e){
e.printStackTrace();
}
return list;
} `这里写代码片`
httpclient请求百度palceAPI
最新推荐文章于 2021-10-29 13:30:14 发布