| | |
| | | import cn.huge.base.common.utils.HttpClientUtils; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.google.common.collect.Maps; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.util.Map; |
| | | |
| | |
| | | * @time: 2021-11-05 16:51:48 |
| | | * @version: 1.0.0 |
| | | */ |
| | | @Slf4j |
| | | public class BaiduMapUtils { |
| | | |
| | | public static void main(String[] args) { |
| | | |
| | | getAddrByLngLat("113.26315561512126", "23.205265046387627", coordtype_bd09ll); |
| | | } |
| | | |
| | | /** |
| | | * 坐标类型,目前支持的坐标类型包括:bd09ll(百度经纬度坐标)、bd09mc(百度米制坐标)、gcj02ll(国测局经纬度坐标,仅限中国)、wgs84ll( GPS经纬度) |
| | | */ |
| | | public static final String coordtype_bd09ll = "bd09ll"; |
| | | public static final String coordtype_bd09mc = "bd09mc"; |
| | | public static final String coordtype_gcj02ll = "gcj02ll"; |
| | | public static final String coordtype_wgs84ll = "wgs84ll"; |
| | | |
| | | /** |
| | | * 百度APi参数 |
| | | */ |
| | | private static final String ak = "roXW0LCjo6P9EnCZgG3O1cBlMzvyapwY"; |
| | | private static final String output = "json"; |
| | | private static final String callback = "showLocation"; |
| | | private static final String geocodingUrl = "http://api.map.baidu.com/geocoding/v3/"; |
| | | private static final String reverseUrl = "http://api.map.baidu.com/reverse_geocoding/v3/"; |
| | | // 李泳江个人账号:15602261488 |
| | | private static final String ak = "roXW0LCjo6P9EnCZgG3O1cBlMzvyapwY"; |
| | | // 王文豪个人账号 |
| | | // private static final String ak = "FhiGsV7BFaHGhGy2IeyPq7GuOiFroQ4o"; |
| | | |
| | | /** |
| | | * 根据详细地址获取坐标值 |
| | |
| | | * @param city 城市 |
| | | * @return JSONObject |
| | | */ |
| | | public static JSONObject getCoordinateByAddr(String address, String city) { |
| | | Map<String, String> parameters = Maps.newHashMap(); |
| | | parameters.put("ak", ak); |
| | | parameters.put("address", address); |
| | | parameters.put("city", city); |
| | | parameters.put("output", output); |
| | | String json = HttpClientUtils.httpGet(geocodingUrl, parameters, null); |
| | | public static JSONObject getLngLatByAddr(String address, String city) { |
| | | String url = geocodingUrl+"?ak="+ak+"&output="+output+"&address="+address+"&city="+city; |
| | | String json = HttpClientUtils.httpGet(url, null, null); |
| | | |
| | | JSONObject data = JSONObject.parseObject(json); |
| | | JSONObject data1=JSONObject.parseObject(data.getString("result")); |
| | |
| | | |
| | | /** |
| | | * 根据坐标获取详细地址 |
| | | * @param lat 经度 |
| | | * @param lng 维度 |
| | | * @param lng 经度 |
| | | * @param lat 维度 |
| | | * @param coordtype 坐标类型,目前支持的坐标类型包括:bd09ll(百度经纬度坐标)、bd09mc(百度米制坐标)、gcj02ll(国测局经纬度坐标,仅限中国)、wgs84ll( GPS经纬度) |
| | | * @return String |
| | | */ |
| | | public static String getAddressByCoordinate(String lat, String lng, String coordtype) { |
| | | Map<String, String> parameters = Maps.newHashMap(); |
| | | parameters.put("location", lat + "," + lng); |
| | | parameters.put("coordtype", coordtype); |
| | | parameters.put("ak", ak); |
| | | parameters.put("output", output); |
| | | String json = HttpClientUtils.httpGet(reverseUrl, parameters, null); |
| | | public static BaiduMapAddrDTO getAddrByLngLat(String lng, String lat, String coordtype) { |
| | | BaiduMapAddrDTO baiduMapAddrDTO = new BaiduMapAddrDTO(); |
| | | try{ |
| | | String location = lat + "," + lng; |
| | | String url = reverseUrl+"?ak="+ak+"&output="+output+"&location="+location+"&coordtype="+coordtype; |
| | | String json = HttpClientUtils.httpGet(url, null, null); |
| | | |
| | | JSONObject data = JSONObject.parseObject(json); |
| | | JSONObject data1=JSONObject.parseObject(data.getString("result")); |
| | | JSONObject data2=JSONObject.parseObject(data1.getString("addressComponent")); |
| | | String district =data2.getString("district"); |
| | | System.out.println(district); |
| | | return district; |
| | | JSONObject data = JSONObject.parseObject(json); |
| | | JSONObject data1 =JSONObject.parseObject(data.getString("result")); |
| | | // 识别地址 |
| | | String formattedAddress = data1.getString("formatted_address"); |
| | | System.out.println("识别地址:"+formattedAddress); |
| | | // 解析地址组成部分 |
| | | JSONObject addressComponent=JSONObject.parseObject(data1.getString("addressComponent")); |
| | | // 国家 |
| | | String country = addressComponent.getString("country"); |
| | | // 省 |
| | | String province = addressComponent.getString("province"); |
| | | // 市 |
| | | String city = addressComponent.getString("city"); |
| | | // 区 |
| | | String district = addressComponent.getString("district"); |
| | | // 街道 |
| | | String town = addressComponent.getString("town"); |
| | | // 路 |
| | | String street = addressComponent.getString("street"); |
| | | // 号 |
| | | String streetNumber = addressComponent.getString("street_number"); |
| | | // 拼接地址 |
| | | String addr = country+province+city+district+town+street+streetNumber; |
| | | System.out.println("拼接地址:"+addr); |
| | | |
| | | baiduMapAddrDTO.setFormattedAddress(formattedAddress); |
| | | baiduMapAddrDTO.setCountry(country); |
| | | baiduMapAddrDTO.setProvince(province); |
| | | baiduMapAddrDTO.setCity(city); |
| | | baiduMapAddrDTO.setDistrict(district); |
| | | baiduMapAddrDTO.setTown(town); |
| | | baiduMapAddrDTO.setStreet(street); |
| | | baiduMapAddrDTO.setStreetNumber(streetNumber); |
| | | baiduMapAddrDTO.setAddr(addr); |
| | | }catch (Exception e){ |
| | | log.error("[BaiduMapUtils.getAddrByLngLat]调用失败,异常信息:"+e, e); |
| | | } |
| | | return baiduMapAddrDTO; |
| | | } |
| | | |
| | | } |