一、通过详细地址获取地理编码
-- 需求:通过地址获取到坐标,记录到数据库
-- 实现:使用高德的地理编码接口,对地址进行编码,拿到经纬度
-- 接口详情:地理/逆地理编码-API文档-开发指南-Web服务 API | 高德地图API
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
@Component
@Slf4j
public class GaoDeUtil {
public static String GEO_URL = "https://restapi.amap.com/v3/geocode/geo?key=%s&address=%s";
@Autowired
private ObjectMapper mObjectMapper;
// 到高德注册申请
@Value("${gaode.map.key}")
private String key;
// 通过地址获取地理编码
public GaoDeResponse getGeoInfo(String address) {
String url;
try {
url = String.format(GEO_URL, key, URLEncoder.encode(address, "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new CommonBusinessException("地址编码UTF-8错误");
}
StringBuffer response = parse(url);
log.info("response : {}", response.toString());
GaoDeBaseResponse result;
try {
// 将响应的json转换成对象
result = mObjectMapper.readValue(response.toString(), GaoDeBaseResponse.class);
} catch (JsonProcessingException e) {
throw new CommonBusinessException("高德地理编码信息转换失败");
}
if ("0".equals(result.getStatus()) || !"OK".equals(result.getInfo())) {
log.error("失败原因:{}", result.getInfo());
throw new CommonBusinessException("高德地理编码请求失败");
}
return result.getGeocodes().get(0);
}
// 获取经纬度
public String[] getLocation(String address) {
return getGeoInfo(address).getLocation().split(",");
}
// 发起请求获取响应数据
private StringBuffer parse(String url) {
BufferedReader in = null;
StringBuffer response = new StringBuffer();
try {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != in) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return response;
}
}
二、以下是相关的类
1.自定义的运行时异常类
import org.springframework.http.HttpStatus;
/**
* 通用业务异常
*/
public class CommonBusinessException extends RuntimeException {
/**
* 异常状态码
*/
private int code = HttpStatus.BAD_REQUEST.value();
/**
* 异常描述信息
*/
private String message;
public CommonBusinessException() {
}
public CommonBusinessException(String message) {
super(message);
this.message = message;
}
public CommonBusinessException(Throwable cause) {
super(cause);
this.message = cause.getMessage();
}
public CommonBusinessException(String message, Throwable cause) {
super(message, cause);
this.message = message;
}
public CommonBusinessException(int code, String message) {
super(message);
this.code = code;
this.message = message;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
@Override
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
2.高德响应基础类
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
@Data
@ApiModel
public class GaoDeBaseResponse {
@ApiModelProperty(value = "返回结果状态值")
private String status;
@ApiModelProperty(value = "返回状态说明")
private String info;
@ApiModelProperty(value = "地理编码信息列表")
private List<GaoDeResponse> geocodes;
}
3.高德地理编码信息类
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel
public class GaoDeResponse {
@ApiModelProperty(value = "国家")
private String country;
@ApiModelProperty(value = "省份")
private String province;
@ApiModelProperty(value = "城市")
private String city;
@ApiModelProperty(value = "城市编码")
private String citycode;
@ApiModelProperty(value = "区/县级名称")
private String district;
@ApiModelProperty(value = "区/县级编码")
private String adcode;
@ApiModelProperty(value = "街道")
private String street;
@ApiModelProperty(value = "门牌")
private String number;
@ApiModelProperty(value = "经纬度信息")
private String location;
@ApiModelProperty(value = "行政级别")
private String level;
}