首页 > 其他 > 详细

通过地址获取百度地图经纬度

时间:2021-04-29 17:57:06      阅读:20      评论:0      收藏:0      [点我收藏+]
  /**
     * 通过地址获取经纬度
     * @param addr
     * @return
     */
    public static Map<String, BigDecimal> getLatAndLngByAddress(String addr) {
        String address = "";
        try {
            address = java.net.URLEncoder.encode(addr,"UTF-8");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        //http://api.map.baidu.com/place/v2/search?ak=80aL08ku8G9rsYCdwCCp9x2EGRZAywF5&output=json&query=%s&region=全国
        String url = String.format("http://api.map.baidu.com/place/v2/search?ak=80aL08ku8G9rsYCdwCCp9x2EGRZAywF5&output=json&query=%s&region=全国",address);
        URL myURL = null;
        URLConnection httpsConn = null;
        //进行转码
        try {
            myURL = new URL(url);
        } catch (MalformedURLException e) {
 
        }
        StringBuffer sb = new StringBuffer();
        try {
            httpsConn = (URLConnection) myURL.openConnection();
            if (httpsConn != null) {
                InputStreamReader insr = new InputStreamReader(
                        httpsConn.getInputStream(), "UTF-8");
                BufferedReader br = new BufferedReader(insr);
                String data = null;
                while ((data = br.readLine()) != null) {
                    sb.append(data);
                }
                insr.close();
            }
        } catch (IOException e) {
 
        }
        Map<String, BigDecimal> map = new HashMap<String, BigDecimal>();
        JSONObject resultJson = JSONObject.fromObject(sb.toString());
        JSONArray jsonArray = (JSONArray)resultJson.get("results");
        JSONObject results0Obj = (JSONObject)jsonArray.get(0);
        JSONObject locationObj = (JSONObject)results0Obj.get("location");
        //纬度
        System.out.println(locationObj.get("lat"));
        //经度
        System.out.println(locationObj.get("lng"));
        return map;
    }
  
需要的pom依赖
      <!--
计算经纬度距离 --> <!-- https://mvnrepository.com/artifact/org.gavaghan/geodesy --> <dependency> <groupId>org.gavaghan</groupId> <artifactId>geodesy</artifactId> <version>1.1.3</version> </dependency>

 

通过地址获取百度地图经纬度

原文:https://www.cnblogs.com/ch94/p/14718499.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!