修改,添加一个重载方法,可以返回省
This commit is contained in:
parent
bf1e35a4d8
commit
5bbbc22e30
|
|
@ -146,6 +146,57 @@ public class AddressCoordinateFormatUtil{
|
|||
return result.getString("formatted_address");
|
||||
}
|
||||
|
||||
/**
|
||||
* 经纬度转地址:调用百度地图Api,传入经纬度,获取地址名称
|
||||
* @author zys
|
||||
* @param longitude 经度 Double
|
||||
* @param latitude 纬度 Double
|
||||
* @return JSONObject 返回整个json,方便获取其他数据
|
||||
*/
|
||||
public static JSONObject coordinateToAddress2(Double longitude, Double latitude){
|
||||
//获取SN
|
||||
Map<String, String> paramsMap = new LinkedHashMap<>();
|
||||
String location = latitude+","+longitude;
|
||||
paramsMap.put("ak", MAP_API_KEY);
|
||||
paramsMap.put("output", "json");
|
||||
paramsMap.put("coordtype", "wgs84ll");
|
||||
paramsMap.put("extensions_poi", "0");
|
||||
paramsMap.put("location", location);
|
||||
String sn = null;
|
||||
try {
|
||||
sn = getLocationSn("/reverse_geocoding/v3?",paramsMap);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
String encoderAddress = null;
|
||||
try {
|
||||
encoderAddress = URLEncoder.encode(location, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
String coordinateToAddressApiUrl = "https://api.map.baidu.com/reverse_geocoding/v3?ak="+MAP_API_KEY+"&output=json&coordtype=wgs84ll&extensions_poi=0&location="+encoderAddress+"&sn="+sn;
|
||||
if (Objects.isNull(longitude) || Objects.isNull(latitude)){
|
||||
throw new RuntimeException("经纬度转地址失败,输入的经纬度不正确");
|
||||
}
|
||||
// 返回结果类似于:
|
||||
// {"status":0,"result":{"location":{"lng":经度值,"lat":纬度值},"formatted_address":"具体地址名称","edz":{"name":""},"business":"科技园,大冲","addressComponent":{"country":"中国","country_code":0,"country_code_iso":"CHN","country_code_iso2":"CN","province":"广东省","city":"深圳市","city_level":2,"district":"南山区","town":"粤海街道","town_code":"440305007","distance":"75","direction":"东北","adcode":"440305","street":"沙河西路","street_number":"2029-西门"},"pois":[],"roads":[],"poiRegions":[],"sematic_description":"","formatted_address_poi":"","cityCode":340}}
|
||||
// "status":0 成功状态
|
||||
String json = requestApi(coordinateToAddressApiUrl);
|
||||
if (json.trim().isEmpty()){
|
||||
throw new RuntimeException("经纬度转地址失败,接口无返回数据:"+json);
|
||||
}
|
||||
// 结果转换为json对象
|
||||
JSONObject obj = JSONObject.parseObject(json);
|
||||
if (!"0".equals(obj.getString("status"))) {
|
||||
throw new RuntimeException("经纬度转地址失败,接口返回状态值为失败:"+json);
|
||||
}
|
||||
JSONObject result= obj.getJSONObject("result");
|
||||
if (Objects.isNull(result)){
|
||||
throw new RuntimeException("经纬度转地址失败,接口返回数据不符合预期,无法获取到result节点:"+json);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求百度地图接口
|
||||
* @author zys
|
||||
|
|
|
|||
Loading…
Reference in New Issue