Java 地名转换成经纬度(百度、腾讯)

百度api调用

    /**
     * description 百度地图逆坐标解析
     *
     * @param
     * @return
     * @exception/throws
     * @date 2020/10/22 上午10:13
     */
    public static LngAndLat getBaiduLngAndLat(String address) {
        LngAndLat lngAndLat = new LngAndLat();
        String url = "http://api.map.baidu.com/geocoder/v2/?address=" + address + "&output=json&ak=yourak";
        String json = loadJSON(url);
        JSONObject obj = JSONObject.fromObject(json);
        if (obj.get("status").toString().equals("0")) {
            double lng = obj.getJSONObject("result").getJSONObject("location").getDouble("lng");
            double lat = obj.getJSONObject("result").getJSONObject("location").getDouble("lat");
            lngAndLat.setLat(lat);
            lngAndLat.setLng(lng);
        } else {
        }
        return lngAndLat;
    }

    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()));
            String inputLine = null;
            while ((inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (IOException ignored) {
        }
        return json.toString();
    }

 

腾讯api调用

    /**
     * description 腾讯逆坐标解析
     * @param
     * @return 火星坐标系
     * @exception/throws
     * @date 2020/10/22 上午10:14
     */
    public static LngAndLat getQQLngANdLat(String address) {
        RestTemplate restTemplate=new RestTemplate();
        String url="https://apis.map.qq.com/ws/geocoder/v1/?address="+address+"&region=北京"+
                "&key=yourkey";
        JSONObject forObject = restTemplate.getForObject(url, JSONObject.class);
        System.out.println(forObject);
        LngAndLat lngAndLat=new LngAndLat();
        double lng = forObject.getJSONObject("result").getJSONObject("location").getDouble("lng");
        double lat = forObject.getJSONObject("result").getJSONObject("location").getDouble("lat");
        lngAndLat.setLat(lat);
        lngAndLat.setLng(lng);
        return lngAndLat;
    }

 

完整类

public class LngAndLatUtil {

    /**
     * description 百度地图逆坐标解析
     *
     * @param
     * @return
     * @exception/throws
     * @date 2020/10/22 上午10:13
     */
    public static LngAndLat getBaiduLngAndLat(String address) {
        LngAndLat lngAndLat = new LngAndLat();
        String url = "http://api.map.baidu.com/geocoder/v2/?address=" + address + "&output=json&ak=yourak";
        String json = loadJSON(url);
        JSONObject obj = JSONObject.fromObject(json);
        if (obj.get("status").toString().equals("0")) {
            double lng = obj.getJSONObject("result").getJSONObject("location").getDouble("lng");
            double lat = obj.getJSONObject("result").getJSONObject("location").getDouble("lat");
            lngAndLat.setLat(lat);
            lngAndLat.setLng(lng);
        } else {
        }
        return lngAndLat;
    }

    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()));
            String inputLine = null;
            while ((inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (IOException ignored) {
        }
        return json.toString();
    }

    /**
     * description 腾讯逆坐标解析
     * @param
     * @return 火星坐标系
     * @exception/throws
     * @date 2020/10/22 上午10:14
     */
    public static LngAndLat getQQLngANdLat(String address) {
        RestTemplate restTemplate=new RestTemplate();
        String url="https://apis.map.qq.com/ws/geocoder/v1/?address="+address+"&region=北京"+
                "&key=yourkey";
        JSONObject forObject = restTemplate.getForObject(url, JSONObject.class);
        System.out.println(forObject);
        LngAndLat lngAndLat=new LngAndLat();
        double lng = forObject.getJSONObject("result").getJSONObject("location").getDouble("lng");
        double lat = forObject.getJSONObject("result").getJSONObject("location").getDouble("lat");
        lngAndLat.setLat(lat);
        lngAndLat.setLng(lng);
        return lngAndLat;
    }

    public static void main(String[] args) {

        LngAndLat lngAndLat = LngAndLatUtil.getQQLngANdLat("北京故宫");
        System.out.println("(经度:" + lngAndLat.getLng() + ",纬度:" + lngAndLat.getLat() + ")");
    }

    @Data
    public static class LngAndLat {
        private Double lng;
        private Double lat;
    }
}

目录

百度api调用

腾讯api调用

完整类


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小吴小吴bug全无

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值