申请百度开放平台KEY(ak)没申请,随便找了一个
参数说明:
region:召回城市
city_limit:区域数据召回限制
这两个参数必须配合使用,city_limit为true时,仅召回region对应区域内数据
/*** @param addressName* @Description 获取坐标* @Throws* @Return java.util.Map<java.lang.String, java.math.BigDecimal>* @Date -03-19 13:53:22* @Author WangKun**/public static Map<String, BigDecimal> getLatAndLngByAddressName(String addressName) {Map<String, BigDecimal> map = new HashMap<>();try {String address = URLEncoder.encode(addressName, "UTF-8");String urlAddress = "http://api./place/v2/search?query="+address+"®ion=泰兴市&city_limit=true&output=json&ak=SfrwGH7INvjPq7BwCrYrioBQZm9XXxrR";StringBuilder sb = new StringBuilder();URL url = new URL(urlAddress);URLConnection connection = url.openConnection();if (connection != null) {InputStreamReader insr = new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8);BufferedReader br = new BufferedReader(insr);String data;while ((data = br.readLine()) != null) {sb.append(data);}insr.close();}String str = sb.toString();if (StringUtils.isNotBlank(str) && StringUtils.isNotEmpty(str)) {JSONObject json = JSON.parseObject(str);if (json.get("message").toString().equals("ok")) {JSONArray jsonArray = (JSONArray) json.get("results");if (null != jsonArray && jsonArray.size() > 0) {// 取最近一个点JSONObject resultObj = (JSONObject) jsonArray.get(0);JSONObject locationObj = (JSONObject) resultObj.get("location");if (null != locationObj) {map.put("lng", (BigDecimal) locationObj.get("lng"));map.put("lat", (BigDecimal) locationObj.get("lat"));}}}}} catch (IOException e) {e.printStackTrace();}return map;}