• 【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();
        }
    }
  • 相关阅读:
    chrome jsonView插件安装
    Android之父Andy Rubin:被乔布斯羡慕嫉妒的天才
    一张图看懂苹果MacBook所有屏幕分辨率
    Mac如何让调整窗口大小更简单
    OS X快捷键小技巧
    magent编译安装及常见错误
    【STL】算法 — partial_sort
    Lucene 4.4 依据Int类型字段删除索引
    简易实现 TextView单行文本水平触摸滑动效果
    cocos2d js 怎样动态载入外部图片
  • 原文地址:https://www.cnblogs.com/jxd283465/p/12518070.html
Copyright © 2020-2023  润新知