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>
  • 相关阅读:
    Unity 类似FingerGestures 的相机跟随功能
    protected 学习
    Unity 学习Json篇
    Unity 动态加载 Prefab
    iTween基础之iTweenPath(自定义路径移动)
    Unity连Photon服务器入门详解
    如何用unity3d实现发送带附件的邮件
    【转】【风宇冲】Unity3D教程宝典之Web服务器篇
    unity Editor编辑器插件脚本学习
    收集整理Android开发所需的Android SDK、开发中用到的工具
  • 原文地址:https://www.cnblogs.com/ch94/p/14718499.html
Copyright © 2011-2022 走看看