【第7篇】通过json-lib去读取百度api地图的地方信息
来源:互联网 发布:战地4同步云数据 编辑:程序博客网 时间:2024/06/06 03:27
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.322987String 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());// addressComponentcity = 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 }})
0 0
- 【第7篇】通过json-lib去读取百度api地图的地方信息
- 【第25篇】通过org.json结合HttpClient解析的百度API附近的银行地方信息
- 【第10篇】通过json-lib的JsonConfig去读取对象数据json
- 【第9篇】通过json-lib去封装的JSON和JAVA的POJO的相互转换JSONHelper.java
- 【第22篇】通过JackJson去读取Json数组数据
- 【第13篇】通过GSON的JsonParser去读取数据处理json数据
- 【第14篇】通过GSON的JsonReader去读取数据处理json数据
- 用百度地图api计算两个地方的距离
- 关于百度地图API的lib包的记录
- 百度地图api路况信息
- 【第1篇】通过json-lib把字符串转换成json的使用
- 百度地图API 点击地图提示信息
- python解析百度地图api返回的json
- 利用百度地图api批量获取地名的详细地址信息
- 通过百度地图API找到地址名的经纬度
- 去百度API的百度地图准确叠加和坐标转换的解决方案研究
- 百度地图API: 通过经纬度定位到百度地图位置
- Android通过百度地图API用Service和Alarm在后台定时获取地理位置信息
- C++函数对象与函数指针不同之处
- Shell编程基础
- 三、字符串、向量和数组
- java多态原理介绍
- CSR 蓝牙芯片运行SPP服务是PS 设置
- 【第7篇】通过json-lib去读取百度api地图的地方信息
- c++ obj文件与exe文件的区别
- pat1004
- Android5.0的更新包中system.new.dat文件的打包
- 将索引移动到别的表空间
- 数据结构实验1——顺序表
- pat1005
- [算法系列] 求生之路(迷宫找出口)
- pat1006