根据经纬度查询地址



import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import cn.eeepay.cache.SystemParamsCache;
import cn.toruk.pub.Log;

/***
 * 根据经纬度,查百度的API得到地址
 * 
 * @author 
 */
public class GetAdressByBaidu {

	/**
	 * @Description:返回JSON格式的,根据经纬度找到的地址 ,传入的经纬度 如:39.917149,116.421516
	 *                                   错误返回,null; 正确返回
	 *                                   例:{"result":{"addressComponent"
	 *                                   :{"street" :"东单三条","province":"北京市",
	 *                                   "street_number"
	 *                                   :"9号","district":"东城区","city"
	 *                                   :"北京市"},"location"
	 *                                   :{"lng":116.421516,"lat"
	 *                                   :39.917149},"cityCode"
	 *                                   :131,"formatted_address"
	 *                                   :"北京市东城区东单三条9号",
	 *                                   "business":"东单,王府井,灯市口"},"status":"OK"}
	 * @author 张红亮
	 * @date 2014-3-27 上午10:19:14
	 */
	public static JSONObject getCoordinate(String latAndLng) {
		JSONObject jsonObject = null;
		if (null != latAndLng && StringUtils.isNotBlank(latAndLng)) {
			StringBuffer resultBuffer = new StringBuffer();
			String key = SystemParamsCache.getParamValue("BAIDU_API_KEY");
			// String key = "=NQWAkrl87NqPEjNjzTYLh3TR";
			String url = String
					.format(
							"http://api.map.baidu.com/geocoder?output=json&location=%s&key=%s",
							latAndLng, key);
			URL baiDuUrl = null;
			URLConnection httpsConn = null;
			InputStreamReader insr = null;
			BufferedReader br = null;
			try {
				baiDuUrl = new URL(url);
				httpsConn = (URLConnection) baiDuUrl.openConnection();// 不使用代理
				if (httpsConn != null) {
					insr = new InputStreamReader(httpsConn.getInputStream(),
							"UTF-8");
					br = new BufferedReader(insr);
					String data = null;
					while ((data = br.readLine()) != null) {
						resultBuffer.append(data);
					}
				}
				jsonObject = new JSONObject(resultBuffer.toString());
			} catch (Exception e) {
				Log.print("取百度的地址错误--》", e);
			} finally {
				try {
					if (insr != null) {
						insr.close();
					}
					if (br != null) {
						br.close();
					}
				} catch (Exception n) {
					Log.print("取百度的地址错误--》", n);
				}
			}
		}
		return jsonObject;
	}

	/**
	 * 将JSON对象,转换后放到MAP并返回
	 * 
	 * @author 张红亮
	 * @date 2014-3-27 上午10:19:14
	 * @param map
	 *            传入要加入值的MAP
	 * @param resultMap
	 *            返回传入的MAP
	 * @return 根据getCoordinate方法取的JSON对象再转为MAP
	 *         例:result--{"addressComponent":{"street"
	 *         :"东单三条","province":"北京市","street_number"
	 *         :"9号","district":"东城区","city"
	 *         :"北京市"},"location":{"lng":116.421516,
	 *         "lat":39.917149},"cityCode":131
	 *         ,"formatted_address":"北京市东城区东单三条9号","business":"东单,王府井,灯市口"}
	 *         status--OK location--{"lng":116.421516,"lat":39.917149}
	 *         street--东单三条 cityCode--131 lng--116.421516 business--东单,王府井,灯市口
	 *         city--北京市
	 *         addressComponent--{"street":"东单三条","province":"北京市","street_number"
	 *         :"9号","district":"东城区","city":"北京市"} province--北京市
	 *         formatted_address--北京市东城区东单三条9号 street_number--9号 district--东城区
	 *         lat--39.917149
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, String> getMapKeyValue(JSONObject map,
			Map<String, String> resultMap) {
		try {
			Iterator it = map.keys();
			while (it.hasNext()) {
				String key = (String) it.next();
				String value = map.getString(key);
				resultMap.put(key, value);
				Object array = map.get(key);
				if (array instanceof JSONObject) {
					getMapKeyValue((JSONObject) array, resultMap);
				}
			}
		} catch (JSONException e) {
			Log.print("解析地址失败!", e);
		}

		return resultMap;
	}

	/**
	 * 根据返回的数据,取到省,市,区的值放入,MAP中并返回
	 * 
	 * @author 张红亮
	 * @date 2014-3-27 上午10:19:14 key---province ,city ,area
	 * @param latAndLng
	 *            经纬度值
	 * @param resultMap
	 *            需传入的MAP
	 * @return
	 */
	public static Map<String, String> getProCitAreValue(String latAndLng,
			Map<String, String> resultMap) {
		Map<String, String> valueMap = getMapKeyValue(getCoordinate(latAndLng),
				resultMap);
		resultMap.put("province", valueMap.get("province"));
		resultMap.put("city", valueMap.get("city"));
		resultMap.put("area", valueMap.get("district"));
		return resultMap;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		JSONObject map = getCoordinate("39.917149,116.421516");
		Map<String, String> map2 = new HashMap<String, String>();
		map2 = getMapKeyValue(map, map2);
		Iterator<String> it = map2.keySet().iterator();
		while (it.hasNext()) {
			String key = (String) it.next();
			String value = (String) map2.get(key);
			System.out.println(key + "--" + value);
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值