准备
首先获取高德地图key
链接:https://lbs.amap.com/api/webservice/guide/create-project/get-key
点击链接进入高德地图开放平台,根据文档获取key。
开发流程
pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>gaode_map</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
</dependencies>
</project>
启动类
package com.test.gaode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.oas.annotations.EnableOpenApi;
/**
* @author清梦
* @site www.xiaomage.com
* @company xxx公司
* @create 2023-05-17 15:04
*/
@SpringBootApplication
@EnableOpenApi
public class GaodeApplication {
public static void main(String[] args) {
SpringApplication.run(GaodeApplication.class,args);
}
}
注册bean,远程调用接口需要用到,也可以用原始的http,或者feign调用。
package com.test.gaode.config;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
/**
* @author清梦
* @site www.xiaomage.com
* @company xxx公司
* @create 2023-05-17 15:39
*/
@Configuration
public class RestTemplateConfig {
@Bean
@Qualifier("restTemplate")
public RestTemplate restTemplate(){
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(10 * 1000)
.setSocketTimeout(3 * 60 * 1000)
.build();
CloseableHttpClient closeableHttpClient = HttpClients.custom()
.setDefaultRequestConfig(requestConfig)
.build();
return new RestTemplate(new HttpComponentsClientHttpRequestFactory(closeableHttpClient));
}
}
配置文件
server:
port: 8094
spring:
application:
name: gaode-map-service
#高德配置
gaode:
key: 自己的key
封装一个调用工具
package com.test.gaode.utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
/**
* @author清梦
* @site www.xiaomage.com
* @company xxx公司
* @create 2023-05-17 17:21
*/
@Component
@Slf4j
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext context = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
/**
* 通过name获取bean
* @param name
* @return
*/
public static Object getBean(String name){
return context.getBean(name);
}
/**
* 通过class获取bean
* @param tClass
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> tClass){
return context.getBean(tClass);
}
/**
* 通过name,class获取bean
* @param name
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(String name,Class<T> clazz){
return context.getBean(name,clazz);
}
public static String doGet(String url){
log.info("请求报文:{}",url);
RestTemplate restTemplate = (RestTemplate)SpringContextUtil.getBean("restTemplate");
String res = restTemplate.getForObject(url, String.class);
log.info("响应报文:{}",res);
return res;
}
}
vo对象
package com.test.gaode.vo;
import lombok.Data;
/**
* @author清梦
* @site www.xiaomage.com
* @company xxx公司
* @create 2023-05-17 15:07
* 地理编码信息
*/
@Data
public class GeoCode {
/**
* 国家,国内地址默认返回中国
*/
private String country;
/**
* 地址所在的省份名,例如:北京市。此处需要注意的是,中国的四大直辖市也算作省级单位。
*/
private String province;
/**
* 地址所在的城市名,例如:北京市
*/
private String city;
/**
* 城市编码,例如:010
*/
private String citycode;
/**
* 地址所在的区,例如:朝阳区
*/
private String district;
/**
* 街道,例如:阜通东大街
*/
private String street;
/**
* 门牌,例如:6号
*/
private String number;
/**
* 区域编码,例如:110101
*/
private String adcode;
/**
* 坐标点,经度,纬度
*/
private String location;
/**
* 匹配级别,参见下方的地理编码匹配级别列表
*/
private String level;
}
接口
地理编码、逆地理编码
package com.test.gaode.controller;
import com.test.gaode.service.GeoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author清梦
* @site www.xiaomage.com
* @company xxx公司
* @create 2023-05-17 15:16
*/
@RestController
@RequestMapping("gaoCode")
@Api(tags = "地理/逆地理编码")
public class GeoController {
@Autowired
private GeoService gaoDeService;
@GetMapping("geo")
@ApiOperation("地理编码")
public ResponseEntity geo(String address){
return gaoDeService.getGeo(address);
}
@GetMapping("reGeo")
@ApiOperation("逆地理编码")
public ResponseEntity reGeo(String location){
return gaoDeService.reGeo(location);
}
}
service
package com.test.gaode.service;
import org.springframework.http.ResponseEntity;
/**
* @author清梦
* @site www.xiaomage.com
* @company xxx公司
* @create 2023-05-17 15:19
*/
public interface GeoService {
ResponseEntity getGeo(String address);
ResponseEntity reGeo(String location);
}
实现类
package com.test.gaode.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.test.gaode.response.GeoResponse;
import com.test.gaode.service.GeoService;
import com.test.gaode.utils.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
/**
* @author清梦
* @site www.xiaomage.com
* @company xxx公司
* @create 2023-05-17 15:20
*/
@Service
@Slf4j
public class GeoServiceImpl implements GeoService {
@Value("${gaode.key}")
private String key;
@Override
public ResponseEntity getGeo(String address) {
String url = "https://restapi.amap.com/v3/geocode/geo";
String reqUrl = url + "?key=" + key + "&address=" + address;
String entity = SpringContextUtil.doGet(reqUrl);
JSONObject jsonObject = JSON.parseObject(entity);
GeoResponse response = jsonObject.toJavaObject(GeoResponse.class);
return ResponseEntity.ok(response);
}
@Override
public ResponseEntity reGeo(String location) {
String url = "https://restapi.amap.com/v3/geocode/regeo";
String reqUrl = url + "?key=" + key + "&location=" + location;
String entity = SpringContextUtil.doGet(reqUrl);
return ResponseEntity.ok(entity);
}
}
通用接口
接口调用方法相同,只是url和参数不同,抽取成通用接口
package com.test.gaode.controller;
import com.test.gaode.service.CommonService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author清梦
* @site www.xiaomage.com
* @company xxx公司
* @create 2023-05-18 10:52
*/
@RestController
@RequestMapping("common")
@Api(tags = "通用接口")
public class CommonController {
@Autowired
private CommonService commonService;
@GetMapping("getInfo")
public ResponseEntity getInfo(String url,String params){
return commonService.getInfo(url,params);
}
}
service
package com.test.gaode.service;
import org.springframework.http.ResponseEntity;
/**
* @author清梦
* @site www.xiaomage.com
* @company xxx公司
* @create 2023-05-18 10:54
*/
public interface CommonService {
ResponseEntity getInfo(String url,String params);
}
实现类
package com.test.gaode.service.impl;
import com.test.gaode.service.CommonService;
import com.test.gaode.utils.SpringContextUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
/**
* @author清梦
* @site www.xiaomage.com
* @company xxx公司
* @create 2023-05-18 10:55
*/
@Service
public class CommonServiceImpl implements CommonService {
@Value("${gaode.key}")
private String key;
@Override
public ResponseEntity getInfo(String url,String params) {
String reqUrl = url + "?key=" + key + "&" + params;
return ResponseEntity.ok(SpringContextUtil.doGet(reqUrl));
}
}
测试效果
运行启动类,在浏览器输入localhost:8094/swagger-ui/,进入swagger页面
进入地理编码接口
点击后侧的“try out”按钮,输入参数,点击执行
获得响应如图
也可以调用通用接口测试