package cn.huge.module.utils;
|
|
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;
|
|
/**
|
* @title: 百度地图工具类
|
* @description: 百度地图工具类
|
* @company: hugeinfo
|
* @author: liyj
|
* @time: 2021-11-05 16:51:48
|
* @version: 1.0.0
|
*/
|
@Slf4j
|
public class BaiduMapUtils {
|
|
public static void main(String[] args) {
|
BaiduMapLngLatDTO baiduMapLngLatDTO = getLngLatByAddr("钟落潭镇五龙岗村", "广州市");
|
BaiduMapAddrDTO baiduMapAddrDTO = getAddrByLngLat(baiduMapLngLatDTO.getLng(), baiduMapLngLatDTO.getLat(), BaiduMapUtils.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 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 address 详细地址
|
* @param city 城市
|
* @return JSONObject
|
*/
|
public static BaiduMapLngLatDTO getLngLatByAddr(String address, String city) {
|
BaiduMapLngLatDTO baiduMapLngLatDTO = new BaiduMapLngLatDTO();
|
try {
|
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"));
|
// 解析地址的经纬度
|
JSONObject lngLatComponent = JSONObject.parseObject(data1.getString("location"));
|
System.out.println(lngLatComponent.toString());
|
// 经度
|
String lng = lngLatComponent.getString("lng");
|
baiduMapLngLatDTO.setLng(lng);
|
// 维度
|
String lat = lngLatComponent.getString("lat");
|
baiduMapLngLatDTO.setLat(lat);
|
}catch (Exception e){
|
log.error("[BaiduMapUtils.getLngLatByAddr]调用失败,异常信息:"+e, e);
|
}
|
return baiduMapLngLatDTO;
|
}
|
|
/**
|
* 根据坐标获取详细地址
|
* @param lng 经度
|
* @param lat 维度
|
* @param coordtype 坐标类型,目前支持的坐标类型包括:bd09ll(百度经纬度坐标)、bd09mc(百度米制坐标)、gcj02ll(国测局经纬度坐标,仅限中国)、wgs84ll( GPS经纬度)
|
* @return String
|
*/
|
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"));
|
// 识别地址
|
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;
|
}
|
|
}
|
/**
|
* -------------------_ooOoo_-------------------
|
* ------------------o8888888o------------------
|
* ------------------88" . "88------------------
|
* ------------------(| -_- |)------------------
|
* ------------------O\ = /O------------------
|
* ---------------____/`---'\____---------------
|
* -------------.' \\| |// `.-------------
|
* ------------/ \\||| : |||// \------------
|
* -----------/ _||||| -:- |||||- \-----------
|
* -----------| | \\\ - /// | |-----------
|
* -----------| \_| ''\---/'' | |-----------
|
* -----------\ .-\__ `-` ___/-. /-----------
|
* ---------___`. .' /--.--\ `. . __----------
|
* ------."" '< `.___\_<|>_/___.' >'"".-------
|
* -----| | : `- \`.;`\ _ /`;.`/ - ` : | |-----
|
* -----\ \ `-. \_ __\ /__ _/ .-` / /-----
|
* ======`-.____`-.___\_____/___.-`____.-'======
|
* -------------------`=---='
|
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
* ---------佛祖保佑---hugeinfo---永无BUG----------
|
*/
|