目录
高德开放平台官网:高德开放平台 | 高德地图APIhttps://lbs.amap.com/
高德开放平台IP地址:IP定位-基础 API 文档-开发指南-Web服务 API | 高德地图API
1、申请一个Web服务器的API密钥
- 头像——> 应用管理 ——> 我的应用 ——>添加key
2、代码调用接口获取用户IP信息即可
- Controller层
@RestController
@RequiredArgsConstructor
public class IpController {
private final ServiceIp serviceIp;
@GetMapping("/getip")
public ArrayList<String> getIpInfo(){
return serviceIp.getIp();
}
}
- Service层
public interface ServiceIp {
ArrayList<String> getIp();
}
- 这里主要是通过一个Map集合封装你的密钥key和用户IP地址
- 通过Hutool的HttpUtil像高德官方API发送get请求
- 最后通过FastJson2解析成对象
@Service
public class ServiceIpImpl implements ServiceIp{
@Override
public ArrayList<String> getIp() {
//调用高德地图API获取用户IP地址的地区信息
Map<String,Object> map=new HashMap<>();
map.put("key","你的api的key密钥");
map.put("ip","114.247.50.2");
String localeResultStr = HttpUtil.get("https://restapi.amap.com/v3/ip", map);
JSONObject localJsonObj = JSON.parseObject(localeResultStr);
ArrayList<String> list = new ArrayList<>();
list.add(localJsonObj.getString("status"));
list.add(localJsonObj.getString("info"));
list.add(localJsonObj.getString("infocode"));
list.add(localJsonObj.getString("province"));
list.add(localJsonObj.getString("city"));
list.add(localJsonObj.getString("adcode"));
list.add(localJsonObj.getString("rectangle"));
return list;
}
}