zoukankan      html  css  js  c++  java
  • 通过地址获取百度地图经纬度

      /**
         * 通过地址获取经纬度
         * @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=你的ak&output=json&query=%s&region=全国
            String url = String.format("http://api.map.baidu.com/place/v2/search?ak=你的ak&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>
  • 相关阅读:
    Alpine linux如何配置和管理自定义服务
    nginx仅允许域名访问禁止IP访问
    解决influxdb的log日志输出位置
    python配置文件INI/TOML/YAML/ENV的区别
    window获取本机所有IP
    学习本来的样子
    yum/编译安装Zabbix 5.0 LTS
    redis问题优化
    解决nginx同端口强制跳转https配置ssl证书问题
    通过DNS验证自动申请nginx证书
  • 原文地址:https://www.cnblogs.com/ch94/p/14718499.html
Copyright © 2011-2022 走看看