根据经纬度获取该地址所属省/市/区/乡镇/街道名称

1.这里演示的代码目的是为了通过经纬度,获取该地址所属乡镇名称、区名、全名。
2.使用了百度地图API接口:
详情:http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding-abroad&qq-pf-to=pcqq.c2c
3.直接上代码:

package grab;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.alibaba.fastjson.JSONObject;

public class town {
	public static void main(String[] args) {
		//String uid = newGUID();
		String town = getAdressDetail(118.726425,31.092391);
		System.out.println(town);
	}
	
	/**
	 * 逆地理编码 获取乡镇名称
	 */
	public static String getAdressDetail(double lng,double lat) {
		String result = "";// 访问返回结果
		BufferedReader read = null;// 读取访问结果
		String town = "";//城镇名称
		String regionScope = "";//城镇范围
		
		String url = "http://api.map.baidu.com/reverse_geocoding/v3/";
		String param = "ak=SfrwGH7INvjPq7BwCrYrioBQZm9XXxrR&output=json&coordtype=wgs84ll&extensions_town=true"+
				"&location="+lat+","+lng;
		//System.out.println("访问地址:" + url + "?" + param);
		try {
			// 创建url
			URL realurl = new URL(url + "?" + param);
			// 打开连接
			URLConnection connection = realurl.openConnection();
			// 设置通用的请求属性
			connection.setRequestProperty("accept", "*/*");
			connection.setRequestProperty("connection", "Keep-Alive");
			connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			// 建立连接
			connection.connect();
			// 获取所有响应头字段
//			Map> map = connection.getHeaderFields();
//			// 遍历所有的响应头字段,获取到cookies等
//			for (String key : map.keySet()) {
//				System.out.println(key + "--->" + map.get(key));
//			}
			// 定义 BufferedReader输入流来读取URL的响应
			read = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
			String line;// 循环读取
			while ((line = read.readLine()) != null) {
				result += line;
			}
			
			//不为空,解析处城镇名称
			if(!"".equals(result)){
				JSONObject jsonInfo = JSONObject.parseObject(result);
				town = JSONObject.parseObject(JSONObject.parseObject(jsonInfo.getString("result")).getString("addressComponent")).getString("town").toString();
				regionScope = JSONObject.parseObject(jsonInfo.getString("result")).getString("formatted_address")+town;
				System.out.println(regionScope);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (read != null) {// 关闭流
				try {
					read.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return town;
	}
	
	
	/**
	 * 产生一个32位的GUID
	 * @return
	 */
	public static String newGUID(){
		UUID uuid = UUID.randomUUID();
		return uuid.toString();
	}
	
}

4.看效果:
根据经纬度获取该地址所属省/市/区/乡镇/街道名称_第1张图片
5.技术点分析:

/*
 *1.刚拿到的result,是一个json格式的字符串,该字符串,有好几层嵌套,所以在取数据的时候,需要一层一层解析
 {"status":0,"result":{"location":{"lng":118.73814909971941,"lat":31.095934413770754},"formatted_address":"安徽省宣城市宣州区","business":"","addressComponent":{"country":"中国","country_code":0,"country_code_iso":"CHN","country_code_iso2":"CN","province":"安徽省","city":"宣城市","city_level":2,"district":"宣州区","town":"养贤乡","adcode":"341802","street":"","street_number":"","direction":"","distance":""},"pois":[],"roads":[],"poiRegions":[],"sematic_description":"","cityCode":190}}
 *2.首先需要将字符串转换成json对象
 *3.获取最外面的json内容:result --JSONObject.parseObject(jsonInfo.getString("result")).toString
 *4.在获取result 里面最外侧的json内容:addressComponent -JSONObject.parseObject(JSONObject.parseObject(jsonInfo.getString("result")).getString("addressComponent")).toString
 *5.最后就能获取乡镇名称了:town--JSONObject.parseObject(JSONObject.parseObject(jsonInfo.getString("result")).getString("addressComponent")).getString("town").toString();
 */

			//不为空,解析处城镇名称
			if(!"".equals(result)){
				JSONObject jsonInfo = JSONObject.parseObject(result);
				town = JSONObject.parseObject(JSONObject.parseObject(jsonInfo.getString("result")).getString("addressComponent")).getString("town").toString();
				regionScope = JSONObject.parseObject(jsonInfo.getString("result")).getString("formatted_address")+town;
				System.out.println(regionScope);
			}

6.jar包。。。。
这里有我的jar包连接,可以直接用。或者复制import后面的工具名去别的地方下载。
https://download.csdn.net/download/weixin_42402326/11661019

你可能感兴趣的:(Demo,经纬度获取地名,逆转)