package com.stylefeng.guns.modular.system.utils;
|
|
import com.google.maps.GeoApiContext;
|
import com.google.maps.GeocodingApi;
|
import com.google.maps.model.AddressComponent;
|
import com.google.maps.model.GeocodingResult;
|
|
public class AddressLookup {
|
|
public static String getAddress(String administrativeCode) {
|
String apiKey = "AIzaSyBBW0XxW1FK7IXmmS7KFtAjX3o99eFPsss";
|
|
// 创建GeoApiContext实例
|
GeoApiContext context = new GeoApiContext.Builder()
|
.apiKey(apiKey)
|
.build();
|
|
// 设置邮政编码
|
String zipcode = administrativeCode;
|
|
try {
|
// 发起逆地理编码请求
|
GeocodingResult[] results = GeocodingApi.geocode(context, zipcode).await();
|
|
// 提取结果
|
if (results.length > 0) {
|
String formattedAddress = results[0].formattedAddress;
|
System.out.println("地址:" + formattedAddress);
|
return formattedAddress;
|
} else {
|
System.out.println("未找到地址");
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
private static String getComponent(AddressComponent[] components, String type) {
|
for (AddressComponent component : components) {
|
if (component.types[0].equals(type)) {
|
return component.longName;
|
}
|
}
|
return null;
|
}
|
}
|