根据地址获取经纬度

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

/**
     * 通过地址获取经纬度
     *
     * @param address
     * @return
     */
    public static Map<String, String> getLngAndLag(String address) {
        Map<String, String> 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<String, String> getArea(String lng, String lat) {
        Map<String, String> 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<String, String> 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<String, String> addressResolution(String address) {

        String[] cityName = {"上海", "北京", "天津", "重庆"};

        String[] cityArr = address.split("市", -1);

        if (ArrayUtils.contains(cityName, cityArr[0])) {
            address = cityArr[0] + "市市辖区" + cityArr[1];
        }

        String regex = "(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(?<county>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)?(?<town>[^区]+区|.+镇)?(?<village>.*)";
        Matcher m = Pattern.compile(regex).matcher(address);
        String province = null, city = null, county = null, town = null, village = null;
        Map<String, String> row = null;
        while (m.find()) {
            row = new LinkedHashMap<String, String>();
            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;
    }

 


版权声明:本文为wyfsxs原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。