申请密钥流程:
控制台->应用管理->我的应用->创建应用->添加Key
API Key安全策略:
- 设置域名白名单:对输入的域名及其子域名授权,默认情况下所有域名授权
- 授权IP:支持单一或多IP授权(多IP段可通过”起始IP-结束IP”配置),默认情况下所有IP授权
- 签名校验:发送请求时会对某些参数加密生成sig放到请求路径中。腾讯服务器在收到请求后,会使用相同的方式生成sig,并与请求中附带的sig进行比对,一致则通过
地理编码:将具体地址转换成经纬度
逆地理编码:将经纬度转化成具体地址
根据请求所需的参数封装包
@Data
public class LocationRequest {
/**
* 详细地址
*/
private String address;
/**
* 纬度
*/
private String lat;
/**
* 经度
*/
private String lng;
}
根据返回的参数封装包(便于接收参数)
@Data
public class ResponseResult {
private String body;
private String statusCode;
private int statusCodeValue;
}
@Data
public class LeadingResultBody implements Serializable {
private String message;
private Integer status;
private Object result;
}
@Data
public class ResultDetail {
private LocationEntity location;
private String address;
private String title;
}
@Data
public class LocationEntity {
private String lat;
private String lng;
}
Service层
@Slf4j
@Service
public class LocationService {
@Autowired
RestTemplate restTemplate;
public final static String BASE_URL =
"https://apis.map.qq.com/ws/geocoder/v1/?key=TP2BZ-6A5CQ-*****&";//key对应密钥
public String setAddress(String address) {
return BASE_URL + "address=" + address;
}
public String setAddress(String lat, String lng) {
return BASE_URL + "location=" + lat + "," + lng;
}
public Object getLocation(LocationRequest locationRequest) {
if (StringUtils.isNoneBlank(locationRequest.getAddress())) {
return getLocationSingle(locationRequest.getAddress());
} else {
return getLocationList(locationRequest);
}
}
/**
* 通过地址获得经纬度信息
*
* @param address
* @return
*/
public Map<String, Object> getLocationSingle(String address) {
Map<String, Object> map = new HashMap<>();
String url = setAddress(address);
String response = JSONObject.toJSONString(sendRequest(url));
ResultDetail resultDetail = JSONObject.parseObject(response, ResultDetail.class);
String location = JSONObject.toJSONString(resultDetail.getLocation());
LocationEntity locationEntity = JSONObject.parseObject(location, LocationEntity.class);
log.debug(resultDetail.toString());
log.debug(locationEntity.toString());
map.put("address", resultDetail.getTitle());
map.put("lat", locationEntity.getLat());
map.put("lng", locationEntity.getLng());
return map;
}
/**
* 通过经纬度获得周围地理信息(目前只实现获取当前坐标下的信息)
*
* @param locationRequest
* @return
*/
public List<Map<String, Object>> getLocationList(LocationRequest locationRequest) {
List<Map<String, Object>> result = new ArrayList<>();
String url = setAddress(locationRequest.getLat(), locationRequest.getLng());
String response = JSONObject.toJSONString(sendRequest(url));
ResultDetail resultDetail = JSONObject.parseObject(response, ResultDetail.class);
String location = JSONObject.toJSONString(resultDetail.getLocation());
LocationEntity locationEntity = JSONObject.parseObject(location, LocationEntity.class);
//目前只获取当前坐标下的信息
Map<String, Object> map = new HashMap<>();
map.put("address", resultDetail.getAddress());
map.put("lat", locationEntity.getLat());
map.put("lng", locationEntity.getLng());
result.add(map);
return result;
}
public Object sendRequest(String url) {
Object result = new Object();
HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.AUTHORIZATION, null);
ResponseEntity<String> entity;
try {
entity = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<String>(headers), String.class);
String response = JSONObject.toJSONString(entity);
ResponseResult resultTemp = JSONObject.parseObject(response, ResponseResult.class);
LeadingResultBody resultBody = JSONObject.parseObject(resultTemp.getBody(), LeadingResultBody.class);
System.out.println(resultBody);
if (resultBody.getStatus().equals(0)) {
result = resultBody.getResult();
System.out.println(result);
}
} catch (Exception e) {
log.info("腾讯地图开放平台调用失败,错误信息" + e);
}
return result;
}
}
Controller层
@RestController
@RequestMapping("/location")
public class LocationController {
@Autowired
LocationService locationService;
@PostMapping
public CommonResponse getLocation(@RequestBody LocationRequest locationRequest) {
return CommonResponse.success(locationService.getLocation(locationRequest));
}
}
完成之后可以测试一下(若同时输入地址和经纬度,默认根据地址查经纬度)