通过地址获取地理相关信息 通过经纬度获取位置相关信息 根据ip获取地理坐标相关信息
@Slf4j
public class StaticTools {
final static String AK = "**************************";
final static String ADDRESS_TO_LONGITUDEA_URL = "http://api.map.baidu.com/geocoding/v3/?output=json&location=showLocation";
final static String LONGITUDE_TO_ADDRESS_URL = "http://api.map.baidu.com/reverse_geocoding/v3/?output=json&coordtype=BD09&pois=1";
public static AdressResult AddressTolongitudea(String address) {
address = "浙江省杭州市西湖区";
if (StringUtils.isBlank(address)) {
}
String url = ADDRESS_TO_LONGITUDEA_URL + "&ak=" + AK + "&address=" + address;
log.info("请求url:" + url);
HttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
try {
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
JSONObject resultsJson = JSONObject.parseObject(result);
AdressResult adressResult = JSONObject.toJavaObject(resultsJson, AdressResult.class);
log.info("返回信息:" + adressResult.toString());
return adressResult;
} catch (Exception e) {
log.error("地理编码[异常],", e);
return null;
}
}
public static LongitudeToAddress longitudeToAddress(float lat, float lng) {
lng=Float.valueOf("120.08191131675022");
lat=Float.valueOf("30.32270208092518");
String url = LONGITUDE_TO_ADDRESS_URL + "&ak=" + AK + "&location=" + lat + "," + lng;
log.info("请求url:" + url);
HttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
try {
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
String results = EntityUtils.toString(entity);
JSONObject resultsJson = JSONObject.parseObject(results);
LongitudeToAddress adressConvert = JSONObject.toJavaObject(resultsJson, LongitudeToAddress.class);
log.info("返回信息:" + results);
return adressConvert;
} catch (Exception e) {
log.error("逆地理编码[异常],", e);
return null;
}
}
public static Location address(String ip) {
try {
URL url = new URL("http://api.map.baidu.com/location/ip?ip=" + ip + "&ak=" + AK +
"&coor=bd09ll");
InputStream inputStream = url.openStream();
InputStreamReader inputReader = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(inputReader);
String results = reader.readLine();
if (!StringUtils.isNotEmpty(results)) {
return null;
}
JSONObject resultsJson = JSONObject.parseObject(results);
IpReturnDateDto ipReturnDateDto = JSONObject.toJavaObject(resultsJson, IpReturnDateDto.class);
Float y = Float.valueOf(ipReturnDateDto.getContent().getPoint().getY());
Float x = Float.valueOf(ipReturnDateDto.getContent().getPoint().getX());
Location location=new Location(x,y);
return location;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) throws IOException {
address(getNowIP());
AddressTolongitudea(null);
longitudeToAddress(0,0);
}
private static String getNowIP() throws IOException {
String ip = null;
String objWebURL = "https://bajiu.cn/ip/";
BufferedReader br = null;
try {
URL url = new URL(objWebURL);
br = new BufferedReader(new InputStreamReader(url.openStream()));
String s = "";
String webContent = "";
while ((s = br.readLine()) != null) {
if (s.indexOf("互联网IP") != -1) {
ip = s.substring(s.indexOf("'") + 1, s.lastIndexOf("'"));
break;
}
}
} finally {
if (br != null)
br.close();
}
if (StringUtils.isEmpty(ip)) {
throw new RuntimeException();
}
return ip;
}
}
转换相关类
通过地址获取地理相关信息
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AdressResult {
private String level;
private Integer precise;
private Integer confidence;
private Integer comprehension;
private Integer status;
private Location location;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AddressTolongitudea {
private Integer status;
private AdressResult result;
}
通过经纬度获取位置相关信息
@Data
@AllArgsConstructor
@NoArgsConstructor
public class LongitudeToAddress {
private Integer status;
private LongitudeaResult result;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class LongitudeaResult {
private String formatted_address;
private String business;
private String sematic_description;
private Integer cityCode;
private Object roads;
private Location location;
private Object poiRegions;
private Object pois;
private AddressComponent addressComponent;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Location {
private Float lng;
private Float lat;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AddressComponent {
private Integer city_level;
private String country;
private String town;
private String distance;
private String city;
private String adcode;
private String country_code_iso;
private String country_code_iso2;
private String country_code;
private String town_code;
private String province;
private String street;
private String district;
private String street_number;
private String direction;
}
|