1.(高德api)获取周边地址信息,根据地址经纬度,距离,关键词查询

This commit is contained in:
hanrenchun 2025-11-18 10:27:41 +08:00
parent 9c51a18310
commit a6f08a7ad2
1 changed files with 70 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
@ -14,6 +15,12 @@ public class AmapGeocoding {
public static String apiKey = "4cc27f0dbf3ebb77a3cf219866b405ad"; public static String apiKey = "4cc27f0dbf3ebb77a3cf219866b405ad";
/**
* 根据地址名称获取地址经纬度
* @param address 详细地址 常熟市海虞北路27号
* @return
*/
public static String getLocation(String address) { public static String getLocation(String address) {
// 对地址进行URL编码处理特殊字符 // 对地址进行URL编码处理特殊字符
String encodedAddress = java.net.URLEncoder.encode(address); String encodedAddress = java.net.URLEncoder.encode(address);
@ -69,9 +76,68 @@ public class AmapGeocoding {
} }
/**
* 获取周边地址信息根据地址经纬度距离关键词查询
* @param centerLocation 中心地址经纬度 120.754471,31.666573
* @param searchRadius 距离 单位 1000
* @param searchKeyword 关键词 停车场
* @return
*/
public static String getPois(String centerLocation,int searchRadius,String searchKeyword) {
try {
// 构建请求URL注意实际应用中需对keyword进行URL编码
String urlStr = String.format("https://restapi.amap.com/v3/place/around?key=%s&location=%s&radius=%d&keywords=%s&sortrule=distance",
apiKey, centerLocation, searchRadius, java.net.URLEncoder.encode(searchKeyword, "UTF-8"));
// 创建URL对象并建立连接
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
// 解析JSON响应
JSONObject jsonResponse = new JSONObject(response.toString());
// 检查请求状态
if ("1".equals(jsonResponse.getStr("status"))) {
JSONArray pois = jsonResponse.getJSONArray("pois");
System.out.println("找到 " + pois.size() + " 个结果:");
// 遍历并输出结果
for (int i = 0; i < pois.size(); i++) {
JSONObject poi = pois.getJSONObject(i);
String name = poi.getStr("name");
String address = poi.getStr("address");
String location = poi.getStr("location"); // 格式: "经度,纬度"
String distance = poi.getStr("distance", "未知"); // 距离中心点的距离
System.out.printf("名称: %s | 地址: %s | 经纬度: %s | 距离: %s米%n",
name, address, location, distance);
}
} else {
String errorInfo = jsonResponse.getStr("info");
System.err.println("请求失败: " + errorInfo);
}
} finally {
conn.disconnect();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return null;
}
public static void main(String[] args) { public static void main(String[] args) {
String location = getLocation("常熟市海虞北路27号"); // String location = getLocation("常熟市海虞北路27号");
System.out.println(location); // System.out.println(location);
// 120.754471,31.666573 // 120.754471,31.666573
getPois("120.754471,31.666573",1000,"常客隆超市");
} }
} }