根据经纬度查询相应的省市区名称

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class GetConfigureInfo {

	public static void main(String[] args) { // lat 39.97646 //log 116.3039
		String add = getAdd("116.3039", "39.97646");
		JSONObject jsonObject = JSONObject.fromObject(add);
		JSONArray jsonArray = JSONArray.fromObject(jsonObject.getString("addrList"));
		JSONObject j_2 = JSONObject.fromObject(jsonArray.get(0));
		String allAdd = j_2.getString("admName");
		String arr[] = allAdd.split(",");
		System.out.println(add);
		System.out.println("省:" + arr[0] + "\n市:" + arr[1] + "\n区:" + arr[2]);
	}

	/**
	 * 
	* @Description: 根据经纬度 查询地址 
	* @param @param log 纬度
	* @param @param lat 经度
	* @return String    String类型的json串
	* @throws
	 */
	public static String getAdd(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 = "";
		BufferedReader in = null;
		try {
			URL url = new URL(urlString);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setRequestMethod("POST");
			in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
			String line;
			while ((line = in.readLine()) != null) {
				res += line + "\n";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return res;
	}
}
 
  

注意:运行时必须连接网络,如果网络不通或网络不好,会报错的.java.net.UnknownHostException.

要引入6个jar包才可以进行json的解析,如果不需要json解析不需要jar包引入.

commons-beanutils-1.8.3.jar,
 commons-collections-3.2.1.jar, 
commons-lang-2.4.jar, 
commons-logging-1.1.1.jar, 
ezmorph-1.0.6.jar,
 json-lib-2.3-jdk15.jar

jar包下载地址:点击打开链接

你可能感兴趣的:(java,json,json解析)