根据地址获取经纬度
发布日期:2022-02-07 06:39:42
浏览次数:12
分类:技术文章
本文共 5083 字,大约阅读时间需要 16 分钟。
有时候在开发中我们会需要根据模糊地址获取经纬度,国家信息中心的api可以帮助我们实现这个功能,通过模糊地址获取经纬度直接上代码:
/** * 通过地址获取经纬度 * * @param address * @return */ public static MapgetLngAndLag(String address) { Map lngLat = new HashMap<>(); String url = " http://api.tianditu.gov.cn/geocoder?ds={'keyWord':'" + address + "'}&tk=您的密钥"; String json = loadJSON(url); JSONObject jsonObject = JSONObject.fromObject(json); String status = getJsonValue("status", jsonObject); String backMsg = getJsonValue("msg", jsonObject); if ("0".equals(status) && "ok".equalsIgnoreCase(backMsg)) { if (StringUtils.isEmpty(getJsonValue("resultType", jsonObject))) { DecimalFormat dFormat = new DecimalFormat("#.00"); String lon = dFormat.format(Double.valueOf(jsonObject.getJSONObject("location").getString("lon"))); String lat = dFormat.format(Double.valueOf(jsonObject.getJSONObject("location").getString("lat"))); lngLat.put("lng", lon); lngLat.put("lat", lat); } if (StringUtils.isNotEmpty(getJsonValue("resultType", jsonObject))) { DecimalFormat dFormat = new DecimalFormat("#.00"); String admin = jsonObject.getString("admin"); String s = admin.replaceAll("\\[", "").replaceAll("]", ""); JSONObject JsonMiddle = JSONObject.fromObject(s); String lon = dFormat.format(Double.valueOf(JsonMiddle.getJSONObject("location").getString("lon"))); String lat = dFormat.format(Double.valueOf(JsonMiddle.getJSONObject("location").getString("lat"))); lngLat.put("lng", lon); lngLat.put("lat", lat); } } if ("101".equals(status) || "404".equals(status)) { lngLat = null; } return lngLat; }
网页转换为json字符串代码:
/** * 请求网页消息转换为Json字符串 * * @param url * @return */ public static String loadJSON(String url) { StringBuffer json = new StringBuffer(); try { URL oracle = new URL(url); URLConnection yc = oracle.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader( yc.getInputStream())); String inputLine = null; while ((inputLine = in.readLine()) != null) { json.append(inputLine); } in.close(); } catch (MalformedURLException e) { } catch (IOException e) { } return json.toString(); }
剔除json key为空的情况:
//剔除json key为空的情况 public static String getJsonValue(String jsonKey, JSONObject jsonObject) { String condition = ""; if (jsonObject.containsKey(jsonKey)) { condition = String.valueOf(jsonObject.get(jsonKey)); } return condition; }
根据经纬度获取省市县代码:
public static MapgetArea(String lng, String lat) { Map area = new HashMap<>(); String url = "http://api.tianditu.gov.cn/geocoder?postStr={'lon':" + lng + ",'lat':" + lat + ",'ver':1}&type=geocode&tk=您的key"; String json = loadJSON(url); JSONObject jsonObject = JSONObject.fromObject(json); String status = getJsonValue("status", jsonObject); String backMsg = getJsonValue("msg", jsonObject); Map lngLat = new HashMap<>(); if ("0".equals(status) && "ok".equalsIgnoreCase(backMsg)) { String address = jsonObject.getJSONObject("result").getJSONObject("addressComponent").getString("city"); area = addressResolution(address); area.put("lng", lng); area.put("lat", lat); } if ("101".equals(status) || "404".equals(status)) { area = null; } return area; }
解析地址代码:
/** * 解析地址 * * @param address * @return */ public static MapaddressResolution(String address) { String[] cityName = {"上海", "北京", "天津", "重庆"}; String[] cityArr = address.split("市", -1); if (ArrayUtils.contains(cityName, cityArr[0])) { address = cityArr[0] + "市市辖区" + cityArr[1]; } String regex = "(? [^省]+自治区|.*?省|.*?行政区|.*?市)(? [^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(? [^县]+县|.+区|.+市|.+旗|.+海域|.+岛)?(? [^区]+区|.+镇)?(? .*)"; Matcher m = Pattern.compile(regex).matcher(address); String province = null, city = null, county = null, town = null, village = null; Map row = null; while (m.find()) { row = new LinkedHashMap (); province = m.group("province").replaceAll("省|市|自治区|壮族|维吾尔|回族", ""); row.put("province", province == null ? "" : province.trim()); city = m.group("city").replaceAll("市|自治州|地区|自治市", ""); row.put("city", city == null ? "" : city.trim()); county = m.group("county"); row.put("county", county == null ? "" : county.trim()); town = m.group("town"); row.put("town", town == null ? "" : town.trim()); village = m.group("village"); row.put("village", village == null ? "" : village.trim()); } return row; }
转载地址:https://blog.csdn.net/wyfsxs/article/details/82746654 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年03月31日 01时34分30秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
TurtleArt美景图
2019-04-26
margin布局
2019-04-26
盒模型之border实践--三角形
2019-04-26
块状元素与内敛元素
2019-04-26
CSS控制段落和文字属性和背景
2019-04-26
Python语言开发工具
2019-04-26
Requests库的入门
2019-04-26
Robots协议
2019-04-26
Python网络爬虫的网站实例
2019-04-26
HTML学习思维导图
2019-04-26
h标签与p标签
2019-04-26
BeautifulSoup库的安装及基本元素
2019-04-26
基于bs4的HTML内容遍历方法
2019-04-26
信息标记与信息提取
2019-04-26
各大网站CSS初始化代码
2019-04-26
正则表达式的基本用法
2019-04-26
Python的Re库(正则表达式)基本用法
2019-04-26
Scrapy爬虫框架
2019-04-26
Anaconda
2019-04-26
NumPy库入门
2019-04-26