一.获取API
申请AK
个人测试可按下图填写
记得申请完之后保留AK,下面会用到
二.代码实现
- 依赖导入
<!-- HttpClient用来发送http请求和接受响应--> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency>
- 代码实现
service层实现
package cn.tedu.cn_tedu_v1.location.service.impl;
import cn.tedu.cn_tedu_v1.location.pojo.vo.LocationResponse;
import cn.tedu.cn_tedu_v1.location.service.ILocationService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.util.Map;
/**
* DATE = 2023/7/7 18:30
*/
@Slf4j
@Configuration
public class LocationServiceImpl implements ILocationService {
@Override
public LocationResponse getLocation(String IP) throws IOException {
// 创建 HttpClient 实例
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
//请求url路径
//"https://api.map.baidu.com/location/ip?ip=111.206.214.37&coor=bd09ll&ak=您的AK"
String url
= "https://api.map.baidu.com/location/ip?ip=" + IP + "&coor=bd09ll&ak=您的AK";
// 创建 HttpGet 请求
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
// 转换为字符串
String json = EntityUtils.toString(entity);
//JSON数据转LocationResponse对象
LocationResponse locationResponse =
JSON.parseObject(json, LocationResponse.class);
log.info("JSON数据转换完的LocationResponse对象数据{}", locationResponse);
return locationResponse;
}
}
接口返回参数示例
{
"address": "CN|北京市|北京市|None|None|100|100",
"content": {
"address": "北京市",
"address_detail": {
"adcode": "110000",
"city": "北京市",
"city_code": 131,
"district": "",
"province": "北京市",
"street": "",
"street_number": ""
},
"point": {
"x": "116.41338370",
"y": "39.91092455"
}
},
"status": 0
}
接受返回参数java对象类实现
package cn.tedu.cn_tedu_v1.location.pojo.vo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import java.util.Map;
/**
*
* 接受百度地图定位定位返回数据
* DATE = 2023/7/7 19:18
*/
@Data
public class LocationResponse {
private String address;
private Map<String,Object> content;
private int status;
public LocationResponse(String address, Map<String, Object> content, int status) {
this.address = address;
this.content = content;
this.status = status;
}
}
controller层代码实现
@Autowired
private ILocationService locationService;
/**
* 接受前端请求获取IP,查询地址
*
* @param request 获取到用户IP
* @return
*/
@GetMapping("ip")
public JsonResult getLocation(@ApiIgnore HttpServletRequest request) throws IOException {
//获取当前访问IP
String ip = request.getRemoteAddr();
log.info("获取到的IP{}", ip);
LocationResponse location = locationService.getLocation(ip);
return JsonResult.ok(location);
}
注:本地测试的话由于IP是私网,从浏览器上获取到的IP为127.0.0.1,无法获取到真实的地理位置,请注d
三.测试