| | |
| | | @Component |
| | | public class GDMapGeocodingUtil { |
| | | |
| | | private String key = "c473445a931ee8c42eef8b63fc3602c5"; |
| | | |
| | | @Autowired |
| | | private RestTemplate restTemplate; |
| | | |
| | | @Autowired |
| | | private IGDInterfaceService gdInterfaceService; |
| | | |
| | | @Autowired |
| | | private GDKeyManager gdKeyManager; |
| | | |
| | | /** |
| | | * 执行GET请求:key次数用完时自动切换可用key重试 |
| | | */ |
| | | private String doGetWithRetry(String baseUrl) { |
| | | String lastResp = null; |
| | | int maxAttempts = 5; |
| | | for (int i = 0; i < maxAttempts; i++) { |
| | | String key = gdKeyManager.getKey(); |
| | | String url = baseUrl + (baseUrl.contains("?") ? "&" : "?") + "key=" + key; |
| | | String resp = restTemplate.getForObject(url, String.class); |
| | | lastResp = resp; |
| | | JSONObject jsonObject; |
| | | try { |
| | | jsonObject = JSON.parseObject(resp); |
| | | } catch (Exception e) { |
| | | return resp; |
| | | } |
| | | if (GDKeyManager.isWebKeyExhausted(jsonObject)) { |
| | | gdKeyManager.reportExhausted(key); |
| | | continue; |
| | | } |
| | | return resp; |
| | | } |
| | | return lastResp; |
| | | } |
| | | |
| | | |
| | | /** |
| | |
| | | return map; |
| | | } |
| | | |
| | | String url = "https://restapi.amap.com/v3/geocode/geo?key=" + key + "&output=JSON"; |
| | | String url = "https://restapi.amap.com/v3/geocode/geo?output=JSON"; |
| | | url += "&address=" + province + (ToolUtil.isNotEmpty(city) ? city : "") + (ToolUtil.isNotEmpty(county) ? county : "") + (ToolUtil.isNotEmpty(address) ? address : ""); |
| | | String forObject = restTemplate.getForObject(url, String.class); |
| | | String forObject = doGetWithRetry(url); |
| | | JSONObject jsonObject = JSON.parseObject(forObject); |
| | | String status = jsonObject.getString("status"); |
| | | List<String> list = new ArrayList<>(); |
| | |
| | | * @throws Exception |
| | | */ |
| | | public Map<String, String> geocode(String lon, String lan) throws Exception{ |
| | | String url = "https://restapi.amap.com/v3/geocode/regeo?key=" + key + "&location=" + lon + "," + lan; |
| | | String forObject = restTemplate.getForObject(url, String.class); |
| | | String url = "https://restapi.amap.com/v3/geocode/regeo?location=" + lon + "," + lan; |
| | | String forObject = doGetWithRetry(url); |
| | | JSONObject jsonObject = JSON.parseObject(forObject); |
| | | Map<String, String> map = new HashMap<>(); |
| | | |