根据经纬度获取地址

根据经纬度获取地址

Posted by wenfengSAT on August 1, 2018

根据经纬度获取地址


 public class CoordsUtil {

	//private static final Logger LOG = LoggerFactory.getLogger(CoordsUtil.class);

	/**
	 * 通过坐标获取地址
	 * 
	 * @param longitude
	 * @param latitude
	 * @return
	 */
	public static String getLocation(String longitude, String latitude) {
		String address = getAddress(longitude, latitude);
		//LOG.info("***" + address);
		JSONObject jsonObject = JSONObject.fromObject(address);
		JSONArray jsonArray = JSONArray.fromObject(jsonObject.getString("addrList"));
		JSONObject j_2 = JSONObject.fromObject(jsonArray.get(0));
		String admName = j_2.getString("admName");
		String name = j_2.getString("name");
		String arr[] = admName.split(",");
		StringBuffer sb = new StringBuffer();
		if (!arr[0].equals(arr[1])) {
			sb.append(arr[0]).append(" ");
		}
		sb.append(arr[1]).append(" ").append(arr[2]).append(" ").append(name);
		return sb.toString();
	}

	private static String getAddress(String log, String lat) {
		// lat 小 log 大
		// 参数解释: 纬度,经度 type 001 (100代表道路,010代表POI,001代表门址,111可以同时显示前三项)
		String urlString = "http://gc.ditu.aliyun.com/regeocoding?l=" + lat + "," + log + "&type=010";
		String res = "";
		try {
			URL url = new URL(urlString);
			java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setRequestMethod("POST");
			java.io.BufferedReader in = new java.io.BufferedReader(
					new java.io.InputStreamReader(conn.getInputStream(), "UTF-8"));
			String line;
			while ((line = in.readLine()) != null) {
				res += line + "\n";
			}
			in.close();
		} catch (Exception e) {
			//LOG.info("error in wapaction,and e is " + e.getMessage());
		}
		//LOG.info(res);
		return res;
	}

}