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();
        }
    }
  • 相关阅读:
    java第一章到第四章
    Linux Foundation Secure Boot System Released
    linux下avr单片机开发:中断服务程序
    【LeetCode】Binary Tree Level Order Traversal II
    java 学习笔记4
    动手写快排
    [置顶] 在Ubuntu下实现一个简单的Web服务器
    mahout算法源码分析之Collaborative Filtering with ALS-WR (四)评价和推荐
    hdu 4758 Walk Through Squares
    MediaInfo源代码分析 2:API函数
  • 原文地址:https://www.cnblogs.com/jxd283465/p/12518070.html
Copyright © 2011-2022 走看看