【第7篇】通过json-lib去读取百度api地图的地方信息

package ivyy.taobao.com.utils;

import java.net.URL;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

/**
 *@Author:liangjilong
 *@Date:2015-1-4
 *@Email:jilongliang@sina.com
 *@Version:1.0
 *@Description
 */
public class GlobalConstants {
	
 
	/***
	 * 获取百度地图的url
	 * @param ak秘钥即你在百度申请的key调用访问地图的key
	 * @param location地方的经纬度
	 * @param format格式(json或者xml)
	 * @return
	 */
	public static String getBaiduMapUrl(String ak,String location,String format){
		StringBuffer buffer=new StringBuffer("http://api.map.baidu.com/geocoder/v2/?");
		String url="";
		buffer.append("ak="+ak);
		buffer.append("&callback=renderReverse");
		buffer.append("&location="+location);
		buffer.append("&output="+format);
		buffer.append("&pois=1");
		url=buffer.toString();
		
		return url;
	}
	
	 
}

package ivyy.taobao.com.domain.jsonlib;

import ivyy.taobao.com.utils.GlobalConstants;
import ivyy.taobao.com.utils.HttpRequestUtils;
import ivyy.taobao.com.utils.IoUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 *@DEMO:json
 *@Java:Test3.java
 *@Date:2015-1-18下午3:03:51
 *@Author:liangjilong
 *@Email:jilongliang@sina.com
 *@Weibo:http://weibo.com/jilongliang
 *@Version:1.0
 *@Description:通过json-lib去读取百度api地图的地方信息
 */
public class Test2 {
	public static void main(String[] args) throws Exception{
		//getDataByJsonLib("URL");
		getDataByJsonLib("FILE");
	}
	
	/**
	 * 使用Json-lib获取json数据
	 * @throws Exception
	 */
	private static void getDataByJsonLib(String flag) throws Exception {
		String responseData="";
		if(flag.equals("URL")){
			//北京市的经纬度39.983424,116.322987
			String url=GlobalConstants.getBaiduMapUrl("请你填写你在百度申请的key", "39.983424,116.322987", "json");
			responseData = HttpRequestUtils.HttpURLConnRequest(url, "GET");
		}else if(flag.equals("FILE")){
			String jsonData="ivyy/taobao/com/domain/jsonlib/jsonmap.json";
			String path = JsonMap.class.getClassLoader().getResource(jsonData).getPath();
			responseData=IoUtils.reader(path);//读取文件的内容
		}
		
		// 处理页面的json数据
		int start = responseData.indexOf("(") + 1;
		responseData = responseData.substring(start, responseData.lastIndexOf(")"));
		JSONObject jsonObject = JSONObject.fromObject(responseData);
		// 变量
		String status = "", result = "", lat = "", lng = "", formatted_address = "", 
			   business = "", city = "", district = "", province = "", street = "", street_number = "";

		//判断是否有这个status节点
		if (jsonObject.has("status") && jsonObject.get("status") != null && jsonObject.get("status") != "") {
			status = jsonObject.get("status").toString();// status节点
		}
		result = jsonObject.get("result").toString();// result节点
		JSONObject location = JSONObject.fromObject(JSONObject .fromObject(result).get("location").toString());//location节点

		lat = location.get("lat").toString();// lat节点
		lng = location.get("lng").toString();// lng节点

		JSONObject formatAddr = JSONObject.fromObject(result);
		formatted_address = formatAddr.get("formatted_address").toString();// formatted_address节点
		business = formatAddr.get("business").toString();// business节点

		// 变量
		JSONObject compObj = JSONObject.fromObject(JSONObject.fromObject(result).get("addressComponent").toString());// addressComponent
		city = compObj.get("city").toString();
		district = compObj.get("district").toString();
		province = compObj.get("province").toString();
		street = compObj.get("street").toString();
		street_number = compObj.get("street_number").toString();

		System.out.println(street_number);
		
		JSONArray poisArr = JSONObject.fromObject(result).getJSONArray("pois");

		if (poisArr != null && poisArr.size() > 0) {

			for (int i = 0; i < poisArr.size(); i++) {
				// 变量
				String addr = "", cp = "", distance = "", name = "", poiType = "", tel = "", uid = "", zip = "";
				JSONObject tempJson = JSONObject.fromObject(poisArr.get(i));
				addr = tempJson.get("addr").toString();
				cp = tempJson.get("cp").toString();
				distance = tempJson.get("distance").toString();
				name = tempJson.get("name").toString();
				poiType = tempJson.get("poiType").toString();
				tel = tempJson.get("tel").toString();
				uid = tempJson.get("uid").toString();
				zip = tempJson.get("zip").toString();

				JSONArray pointArr = poisArr.fromObject(tempJson.get("point"));// 获取ponit
				// 变量
				String x = "", y = "";
				for (int j = 0; j < pointArr.size(); j++) {
					JSONObject obj = JSONObject.fromObject(pointArr.get(j));
					x = obj.get("x").toString();
					y = obj.get("x").toString();
					
				}
			}
		}

	}
}

renderReverse&&renderReverse(
	{
    "status": 0,
    "result": {
        "location": {
            "lng": 116.32298703399,
            "lat": 39.983424051248
        },
        "formatted_address": "北京市海淀区中关村大街27号1101-08室",
        "business": "中关村,人民大学,苏州街",
        "addressComponent": {
            "city": "北京市",
            "direction": "附近",
            "distance": "7",
            "district": "海淀区",
            "province": "北京市",
            "street": "中关村大街",
            "street_number": "27号1101-08室"
        },
        "pois": [
            {
                "addr": "北京市海淀区海淀路35号",
                "cp": "NavInfo",
                "direction": "附近",
                "distance": "25",
                "name": "中关村大厦",
                "poiType": "商务大厦",
                "point": {
                    "x": 116.32285606105,
                    "y": 39.983568897877
                },
                "tel": "",
                "uid": "3c08da2d19c0964d9b005001",
                "zip": ""
            },
            {
                "addr": "北京市海淀区中关村大街27号1101-08室",
                "cp": "NavInfo",
                "direction": "附近",
                "distance": "1",
                "name": "眉州东坡酒楼中关村店",
                "poiType": "餐饮",
                "point": {
                    "x": 116.32298182382,
                    "y": 39.983416864194
                },
                "tel": "",
                "uid": "95540906d327cb7527d2bb0a",
                "zip": ""
            },
            {
                "addr": "北京市海淀区中关村大街27号1101-08室",
                "cp": "NavInfo",
                "direction": "附近",
                "distance": "1",
                "name": "中国人民财产保险中关村营业部",
                "poiType": "金融",
                "point": {
                    "x": 116.32298182382,
                    "y": 39.983416864194
                },
                "tel": "",
                "uid": "04b48c6cd2a493b92a06ce64",
                "zip": ""
            },
            {
                "addr": "北京市海淀区海淀路35号",
                "cp": "NavInfo",
                "direction": "附近",
                "distance": "41",
                "name": "中国建设银行中关村支行",
                "poiType": "金融",
                "point": {
                    "x": 116.32291894244,
                    "y": 39.98370711002
                },
                "tel": "",
                "uid": "4eabebb07d31784b4d6d461f",
                "zip": ""
            },
            {
                "addr": "北京市海淀区知春路113号1706号",
                "cp": "NavInfo",
                "direction": "西",
                "distance": "498",
                "name": "银网中心",
                "poiType": "商务大厦",
                "point": {
                    "x": 116.32720385972,
                    "y": 39.982269689954
                },
                "tel": "",
                "uid": "fd8f491140a0a7beadee7a37",
                "zip": ""
            },
            {
                "addr": "北京市海淀区中关村大街27号",
                "cp": "NavInfo",
                "direction": "附近",
                "distance": "28",
                "name": "中国建设银行ATM",
                "poiType": "金融",
                "point": {
                    "x": 116.3229458916,
                    "y": 39.983617272159
                },
                "tel": "",
                "uid": "2c7d2dbe35f607d3cafa5fbe",
                "zip": ""
            },
            {
                "addr": "北京市海淀区中关村大街25号",
                "cp": "NavInfo",
                "direction": "附近",
                "distance": "43",
                "name": "中关村大厦停车场",
                "poiType": "交通设施",
                "point": {
                    "x": 116.32334114602,
                    "y": 39.983292472745
                },
                "tel": "",
                "uid": "d74b272e19c0964d9b0050ff",
                "zip": ""
            },
            {
                "addr": "北京市海淀区中关村大街23号",
                "cp": "NavInfo",
                "direction": "西",
                "distance": "115",
                "name": "中国科学院黄庄小区",
                "poiType": "地产小区",
                "point": {
                    "x": 116.32396995988,
                    "y": 39.983672557011
                },
                "tel": "",
                "uid": "3594ec2c0c19ca50a3d23036",
                "zip": ""
            },
            {
                "addr": "北京市海淀区丹棱街甲1",
                "cp": "NavInfo",
                "direction": "东南",
                "distance": "296",
                "name": "欧美汇购物中心",
                "poiType": "购物",
                "point": {
                    "x": 116.32159843334,
                    "y": 39.985172141413
                },
                "tel": "",
                "uid": "7cbcc1af640ac86e744ff1b7",
                "zip": ""
            },
            {
                "addr": "北京市海淀区中关村大街19号-3层-l310号",
                "cp": "NavInfo",
                "direction": "东南",
                "distance": "185",
                "name": "新中关大厦",
                "poiType": "商务大厦",
                "point": {
                    "x": 116.32194877248,
                    "y": 39.984425808622
                },
                "tel": "",
                "uid": "001cbcab42491f56d92f281e",
                "zip": ""
            }
        ],
        "poiRegions": [],
        "cityCode": 131
    }
}
)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱学习的蹭蹭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值