官方文档 使用 API 前请您先注册高德账号申请Key
逆地理编码
import com. alibaba. fastjson. JSONObject ;
import org. slf4j. Logger ;
import org. slf4j. LoggerFactory ;
import org. springframework. stereotype. Component ;
import java. net. URL;
@Component
public class AddressUntils {
private final static Logger logger = LoggerFactory . getLogger ( AddressUntils . class ) ;
public static String getAddress ( String key, String log, String lat) {
String urlString = "https://restapi.amap.com/v3/geocode/regeo?location=" + log+ "," + lat+ "&extensions=base&batch=false&roadlevel=0&key=" + key;
String res = "" ;
try {
URL url = new URL ( urlString) ;
java. net. HttpURLConnection conn = ( java. net. HttpURLConnection) url. openConnection ( ) ;
conn. setDoOutput ( true ) ;
conn. setRequestMethod ( "GET" ) ;
java. io. BufferedReader in = new java. io. BufferedReader( new java. io. InputStreamReader( conn. getInputStream ( ) , "UTF-8" ) ) ;
String line;
while ( ( line = in. readLine ( ) ) != null ) {
res += line+ "\n" ;
}
in. close ( ) ;
JSONObject jsonObject = JSONObject . parseObject ( res) ;
String infocode = ( String ) jsonObject. get ( "infocode" ) ;
if ( ! infocode. equals ( "10000" ) ) {
logger. info ( "获取地址信息异常,错误码:{}" , infocode) ;
}
JSONObject jsonObject1 = jsonObject. getJSONObject ( "regeocode" ) ;
res = jsonObject1. getString ( "formatted_address" ) ;
} catch ( Exception e) {
logger. error ( "获取地址信息异常{}" , e. getMessage ( ) ) ;
return null ;
}
return res;
}
public static void main ( String [ ] args) {
String address = getAddress ( "你申请的key" , "120.645164" , "31.314322" ) ;
System . out. println ( address) ;
}
}