From 434088b7d4ee12c1206cfb194da2b600f4815f94 Mon Sep 17 00:00:00 2001 From: liujie <liujie> Date: 星期二, 15 八月 2023 18:27:37 +0800 Subject: [PATCH] broker 卡车公司端 --- src/main/java/com/stylefeng/guns/modular/system/utils/AddressLookup.java | 62 ++++++++++++------------------- 1 files changed, 24 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/stylefeng/guns/modular/system/utils/AddressLookup.java b/src/main/java/com/stylefeng/guns/modular/system/utils/AddressLookup.java index 45782f6..65e1ffd 100644 --- a/src/main/java/com/stylefeng/guns/modular/system/utils/AddressLookup.java +++ b/src/main/java/com/stylefeng/guns/modular/system/utils/AddressLookup.java @@ -8,46 +8,32 @@ public class AddressLookup { public static String getAddress(String administrativeCode) { + String apiKey = "AIzaSyBBW0XxW1FK7IXmmS7KFtAjX3o99eFPsss"; + + // 创建GeoApiContext实例 GeoApiContext context = new GeoApiContext.Builder() - .apiKey("AIzaSyBBW0XxW1FK7IXmmS7KFtAjX3o99eFPsss") // REPLACE WITH YOUR API KEY - .build(); - - GeocodingResult[] results = GeocodingApi.geocode(context, "EN " + administrativeCode).awaitIgnoreError(); - - if (results == null || results.length == 0) { - return null; + .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(); } - - AddressComponent[] components = results[0].addressComponents; - String province = getComponent(components, "administrative_area_level_1"); - String city = getComponent(components, "locality"); - String district = getComponent(components, "administrative_area_level_3"); - String street = getComponent(components, "route"); - String number = getComponent(components, "street_number"); - - StringBuilder builder = new StringBuilder(); - - if (province != null) { - builder.append(province); - } - - if (city != null && !city.equals(province)) { - builder.append(city); - } - - if (district != null && !district.equals(city)) { - builder.append(district); - } - - if (street != null) { - builder.append(street); - } - - if (number != null) { - builder.append(number); - } - - return builder.toString(); + return null; } private static String getComponent(AddressComponent[] components, String type) { -- Gitblit v1.7.1