根据地名查询经纬度

public class GetCoordinateUtil {


    public static void main(String[] args) {
        try {
            String[] coordinate = new GetCoordinateUtil().getCoordinate("天安门");
            System.out.println(coordinate[0]+"  "+coordinate[1]);//0:经度 1:纬度
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String, Object> map = getURLContent("天安门");
        System.out.println("lng=" + map.get("lng") + ",lat=" + map.get("lat"));
    }


    /**
     * 根据城市名称查询所在经纬度  百度地图
     * @return
     * @throws IOException
     */
    public String[] getCoordinate(String addr) throws IOException {
        String lng = null;//经度
        String lat = null;//纬度
        String address = null;
        try {
            address = java.net.URLEncoder.encode(addr, "UTF-8");
        }catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        String key = "自己的KEY"; //这是我的(改过你用不了的) NcMnc57RX48Mjps4fP4ZEW5GVHmCCmeg
        String url = String .format("http://api.map.baidu.com/geocoder?address=%s&output=json&key=%s", address, key);
        URL myURL = null;
        URLConnection httpsConn = null;
        try {
            myURL = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        InputStreamReader insr = null;
        BufferedReader br = null;
        try {
            httpsConn = (URLConnection) myURL.openConnection();// 不使用代理
            if (httpsConn != null) {
                insr = new InputStreamReader( httpsConn.getInputStream(), "UTF-8");
                br = new BufferedReader(insr);
                String data = null;
                int count = 1;
                while((data= br.readLine())!=null){
                    if(count==5){
                        lng = (String)data.subSequence(data.indexOf(":")+1, data.indexOf(","));//经度
                        count++;
                    }else if(count==6){
                        lat = data.substring(data.indexOf(":")+1);//纬度
                        count++;
                    }else{
                        count++;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(insr!=null){
                insr.close();
            }
            if(br!=null){
                br.close();
            }
        }
        return new String[]{lng,lat};
    }

    /***
     *
     * @descibe 根据位置获取经纬度信息  腾讯地图
     */
    public static Map<String, Object> getURLContent(String address) {
        //这里需要使用你的key值
        String urlStr = "https://apis.map.qq.com/ws/geocoder/v1/?address=" + address + "&key=你的KEY";//这是我的(改过你用不了的)CRMBZ-P3DHF-MUVJJ-N25J6-6LL6F-JRBMV
        
        //请求的url
        URL url = null;
        //请求的输入流
        BufferedReader in = null;
        //输入流的缓冲
        StringBuffer sb = new StringBuffer();
        try {
            url = new URL(urlStr);
            in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
            String str = null;
            //一行一行进行读入
            while ((str = in.readLine()) != null) {
                sb.append(str);
            }
        } catch (Exception ex) {

        } finally {
            try {
                if (in != null) {
                    in.close(); //关闭流
                }
            } catch (IOException ex) {

            }
        }
        String result = sb.toString();
        String r = JSONObject.parseObject(result).getString("result");
        String location = JSONObject.parseObject(r).getString("location");
        String lng = JSONObject.parseObject(location).getString("lng");
        String lat = JSONObject.parseObject(location).getString("lat");
        Map<String, Object> map = new HashMap<>();
        map.put("lng", lng);
        map.put("lat", lat);
        return map;
    }
}
个人感觉 腾讯不如百度 查询精准,
拿 “天安门” 举例,百度能搜到,腾讯搜不到
另外,百度地图的经纬度和腾讯地图的经纬度 数据是反的,具体位置也有些许差别
例:查询“天津市”
百度 117.208093,39.091103
腾讯 117.30983,39.71755
需要把腾讯的反过来 39.71755,117.30983
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小可乐-我一直在

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

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

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

打赏作者

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

抵扣说明:

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

余额充值