public static AMapAddress getAddress(String addressStr, String city) { AMapAddress address = null; String url = MessageFormat.format(aMapGeoUrl2, addressStr, city); String str = HttpUtils.get(url); try { if (StringUtils.isNotBlank(str)) { JSONObject json = JSONObject.parseObject(str); if (null != json) { int status = json.getIntValue("status"); if (status == 1) { JSONArray geocodes = json.getJSONArray("geocodes"); if (null != geocodes && geocodes.size() > 0) { JSONObject jsonAddress = geocodes.getJSONObject(0); address = new AMapAddress(); address.setCity(jsonAddress.getString("city")); address.setCityCode(jsonAddress.getString("citycode")); address.setProvince(jsonAddress.getString("province")); } } } } } catch (Exception e) { e.printStackTrace(); } return address; }
/** 根据经纬度获取地址 */ public static AMapAddress getAddress(double longitude, double latitude) { AMapAddress address = null; String url = MessageFormat.format(aMapUrl, String.valueOf(longitude), String.valueOf(latitude)); String str = HttpUtils.get(url); try { if (StringUtils.isNotBlank(url)) { JSONObject json = JSONObject.parseObject(str); if (null != json) { int status = json.getIntValue("status"); if (status == 1) { JSONObject regeocode = json.getJSONObject("regeocode"); if (null != regeocode) { JSONObject addressComponent = regeocode.getJSONObject("addressComponent"); if (null != addressComponent) { // city 值不固定 String province = addressComponent.getString("province"); String citycode = addressComponent.getString("citycode"); String city = addressComponent.getString("city"); if (StringUtils.isNotBlank(city) && city.equals("[]")) { city = ""; } address = new AMapAddress(); address.setCityCode(citycode); address.setProvince(province); address.setCity(city); } } } } } } catch (Exception e) { } return address; }