根据地址获取经纬度
发布日期:2022-02-07 06:39:42 浏览次数:12 分类:技术文章

本文共 5083 字,大约阅读时间需要 16 分钟。

有时候在开发中我们会需要根据模糊地址获取经纬度,国家信息中心的api可以帮助我们实现这个功能,通过模糊地址获取经纬度直接上代码:

/**     * 通过地址获取经纬度     *     * @param address     * @return     */    public static Map
getLngAndLag(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 Map
getArea(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 Map
addressResolution(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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:PKUSC预热__被水题虐QoQ
下一篇:MongoDB常用查询语句

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年03月31日 01时34分30秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章