zoukankan      html  css  js  c++  java
  • 【Java】百度地图api

    import cc.mrbird.febs.common.utils.HttpClientUtil;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.text.DecimalFormat;
    
    public class BaiduMapAPi {
    
        //Baidu地图api密钥
        private static final String ak = "******************";
    
        // 调用百度地图API根据地址,获取坐标
        public static String getCoordinate(String address) throws JSONException {
            if (address != null && !"".equals(address)) {
                address = address.replaceAll("\s*", "").replace("#", "栋");
                String url = "http://api.map.baidu.com/geocoder/v2/?address=" + address + "&output=json&ak=" + ak;
                String json = loadJSON(url);
                if (json != null && !"".equals(json)) {
                    JSONObject obj = null;
                    obj = new JSONObject(json);
                    if ("0".equals(obj.getString("status"))) {
                        double lng = obj.getJSONObject("result").getJSONObject("location").getDouble("lng"); // 经度
                        double lat = obj.getJSONObject("result").getJSONObject("location").getDouble("lat"); // 纬度
                        DecimalFormat df = new DecimalFormat("#.#####");
                        return df.format(lat) + "," + df.format(lng);
                    }
                }
            }
            return null;
        }
    
    
        // 调用百度地图API根据坐标获取地址
        public static String getReverseCoordinate(String location) {
            String city = null;
            if (location != null) {
                String result = HttpClientUtil.doGet(
                        "http://api.map.baidu.com/geocoder/v2/?ak=" + ak + "&output=json&pois=l&location="
                                + location);
                com.alibaba.fastjson.JSONObject jsonObjectAdds = com.alibaba.fastjson.JSONObject.parseObject(result);
                String province = jsonObjectAdds.getJSONObject("result").getJSONObject("addressComponent")
                        .getString("province");//
                city = jsonObjectAdds.getJSONObject("result").getJSONObject("addressComponent").getString("city");//
    
                System.out.println("province:" + province);
                System.out.println("city:" + city);
            }
            return city;
        }
    
        public static String loadJSON(String url) {
            StringBuilder json = new StringBuilder();
            try {
                URL oracle = new URL(url);
                URLConnection yc = oracle.openConnection();
                BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(), "UTF-8"));
                String inputLine = null;
                while ((inputLine = in.readLine()) != null) {
                    json.append(inputLine);
                }
                in.close();
            } catch (MalformedURLException e) {
            } catch (IOException e) {
            }
            return json.toString();
        }
    }
  • 相关阅读:
    【转】[Python小记] 通俗的理解闭包 闭包能帮我们做什么?
    【OCR技术系列之六】文本检测CTPN的代码实现
    002. Centos7安装mysql5.5.37
    044. asp.net主题之二为主题添加CSS样式和动态加载主题
    043. asp.net主题之一初识主题和皮肤
    042. asp.net使用缓存来提高母版页的访问性能
    041. asp.net中内容页访问母版页中的控件
    040. asp.netWeb中TreeView控件绑定XML文件
    039. asp.netWeb用户控件之七实现具有虚拟键盘的功能的用户控件
    038. asp.netWeb用户控件之六实现日期选择的用户控件
  • 原文地址:https://www.cnblogs.com/jxd283465/p/12518070.html
Copyright © 2011-2022 走看看