-------------------使用百度地图开发平台(lbs),根据经纬度查询商圈出现的问题----------------------


public class BusinessUtil {
public static String getBusniss(String lonAndLat) throws Exception{
// 计算sn跟参数对出现顺序有关,get请求请使用LinkedHashMap保存<key,value>,该方法根据key的插入顺序排序;post请使用TreeMap保存<key,value>,该方法会自动将key按照字母a-z顺序排序。所以get请求可自定义参数顺序(sn参数必须在最后)发送请求,但是post请求必须按照字母a-z顺序填充body(sn参数必须在最后)。以get请求为例:http://api.map.baidu.com/geocoder/v2/?address=百度大厦&output=json&ak=yourak,paramsMap中先放入address,再放output,然后放ak,放入顺序必须跟get请求中对应参数的出现顺序保持一致。
Map paramsMap = new LinkedHashMap<String, String>();
//paramsMap.put("address", "百度大厦");
paramsMap.put("callback", "renderReverse");
paramsMap.put("location", lonAndLat);
paramsMap.put("output", "json");
paramsMap.put("pois", "1");
paramsMap.put("extensions_town", "true");
paramsMap.put("ak", "DE7BNpugy5r5AgYmXNvTxDLMGd622GTP");
// 调用下面的toQueryString方法,对LinkedHashMap内所有value作utf8编码,拼接返回结果address=%E7%99%BE%E5%BA%A6%E5%A4%A7%E5%8E%A6&output=json&ak=yourak
String paramsStr = toQueryString(paramsMap);
// 对paramsStr前面拼接上/geocoder/v2/?,后面直接拼接yoursk得到/geocoder/v2/?address=%E7%99%BE%E5%BA%A6%E5%A4%A7%E5%8E%A6&output=json&ak=yourakyoursk
String wholeStr = new String("/reverse_geocoding/v3/?" + paramsStr + "hYk2oKMy3rATi3OhBMVYhj2AtmTWsmTu");
// 对上面wholeStr再作utf8编码
String tempStr = URLEncoder.encode(wholeStr, "UTF-8");
String sn = MD5(tempStr);
//String url = "http://api.map.baidu.com"+ tempStr + "&sn="+ sn;
String url = "http://api.map.baidu.com/reverse_geocoding/v3/?"+paramsStr + "&sn=" + sn;
//调用HttpClient访问Baidu LBS 百度地图开放平台
HttpClient httpClient = new HttpClient();
GetMethod get = new GetMethod(url);
int status = httpClient.executeMethod(get);
String business = "";
if(status == 200){
String response = get.getResponseBodyAsString();
response = response.replaceAll("renderReverse&&renderReverse\\(","");
response = response.substring(0,response.length()-1);
JSONObject jo = JSON.parseObject(response);
JSONObject result = jo.getJSONObject("result");
//获取商圈
business = result.getString("business");
//如果商圈为空,获取具体的地址最小到镇
if(StringUtils.isEmpty(business)){
StringBuffer buffer = new StringBuffer();
JSONObject addr = result.getJSONObject("addressComponent");
String province = addr.getString("province");
String city = addr.getString("city");
String district = addr.getString("district");
String town = addr.getString("town");
if(StringUtils.isNotEmpty(province)){
buffer.append(province+";");
}
if(StringUtils.isNotEmpty(province)){
buffer.append(city+";");
}
if(StringUtils.isNotEmpty(province)){
buffer.append(district+";");
}
if(StringUtils.isNotEmpty(province)){
buffer.append(town);
}
business = buffer.toString();
}
}
return business;
}

// 对Map内所有value作utf8编码,拼接返回结果
public static String toQueryString(Map<?, ?> data)
throws UnsupportedEncodingException {
StringBuffer queryString = new StringBuffer();
for (Map.Entry<?, ?> pair : data.entrySet()) {
queryString.append(pair.getKey() + "=");
queryString.append(URLEncoder.encode((String) pair.getValue(),
"UTF-8") + "&");
}
if (queryString.length() > 0) {
queryString.deleteCharAt(queryString.length() - 1);
}
return queryString.toString();
}

// 来自*的MD5计算方法,调用了MessageDigest库函数,并把byte数组结果转换成16进制
public static String MD5(String md5) {
try {
java.security.MessageDigest md = java.security.MessageDigest
.getInstance("MD5");
byte[] array = md.digest(md5.getBytes());
StringBuffer sb = new StringBuffer();
for (int i = 0; i < array.length; ++i) {
sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100)
.substring(1, 3));
}
return sb.toString();
} catch (java.security.NoSuchAlgorithmException e) {
}
return null;
}

public static void main(String[] args) throws Exception{
System.out.println(BusinessUtil.getBusniss("40.499603,116.420812"));
//打印结果
//北京市;北京市;延庆区;四海镇
}
}

-------------------使用百度地图开发平台(lbs),根据经纬度查询商圈出现的问题----------------------

 

 上面代码出现问题的解决方法:

-------------------使用百度地图开发平台(lbs),根据经纬度查询商圈出现的问题----------------------

 

上一篇:[二分]煤气灶


下一篇:Oracle学习笔记