百度地图Java地址解析和经纬度解析

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

import com.alibaba.fastjson.JSONObject;

public class BaiduMapUtils {

	public static String MAP_AK = "秘钥AK";

	public static String MAP_URL = "http://api.map.baidu.com/geocoder/v2/?output=json&ak=" + MAP_AK;

	/**
	 * 将经纬度获取解析成详细地址
	 * 
	 * @param lng
	 *            经度
	 * @param lat
	 *            纬度
	 * @return
	 */
	public static String getAddress(double lng, double lat) {
		String address = "";
		String location = lat + "," + lng;
		BufferedReader in = null;
		URL url = null;
		URLConnection connection = null;
		try {
			url = new URL(MAP_URL + "&location=" + location);
			connection = url.openConnection();
			connection.setDoOutput(true);
			in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
			String line;
			StringBuilder text = new StringBuilder("");
			while ((line = in.readLine()) != null) {
				text.append(line.trim());
			}
			JSONObject result = JSONObject.parseObject(text.toString());
			if (result != null && result.getIntValue("status") == 0) {
				address = result.getJSONObject("result").getString("formatted_address");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return address;
	}

	/**
	 * 将地址解析成经纬度
	 * 
	 * @param address
	 *            地址,例:浙江省杭州市西湖区
	 * @return 返回经纬度数据。例:{"lng":120.08899292561351,"lat":30.207036169515438}
	 */
	public static JSONObject getPosition(String address) {
		BufferedReader in = null;
		URL url = null;
		URLConnection connection = null;
		try {
			url = new URL(MAP_URL + "&address=" + address);
			connection = url.openConnection();
			connection.setDoOutput(true);
			in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
			String line;
			StringBuilder text = new StringBuilder("");
			while ((line = in.readLine()) != null) {
				text.append(line.trim());
			}
			JSONObject result = JSONObject.parseObject(text.toString());
			if (result != null && result.getIntValue("status") == 0) {
				return result.getJSONObject("result").getJSONObject("location");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		System.out.println(getAddress(120.08899292561351, 30.207036169515438));
		System.out.println(getPosition("浙江省杭州市西湖区"));
	}

}

 

你可能感兴趣的:(java,百度地图)