zoukankan      html  css  js  c++  java
  • Java高德地图两点间距离、地理编码、逆地理编码

    请注意编码方式一定要是UTF-8,否则地理编码返回count为0,并且报错:

    Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:exec (default-cli) on project education: Command execution failed.

    若遇到这个问题参考:https://www.cnblogs.com/shoose/p/15241828.html

    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    
    
    /**
     * @program: 
     * @description: 高德地图工具类
     * @author: SaffiChan
     * @create: 2021-09-07 14:18
     **/
    public class GaodeUtil {
    
        static String key = ""; //在高德地图控制台我的应用添加应用获取key
    
    
        /**
         * 获取两点间距离,单位米
         *
         * @param startLonLat
         * @param endLonLat
         * @return
         */
        public static Long getDistance(String startLonLat, String endLonLat) {
            //返回起始地startAddr与目的地endAddr之间的距离,单位:米
            Long result = 0L;
            String queryUrl = "http://restapi.amap.com/v3/distance?key=" + key + "&origins=" + startLonLat + "&destination=" + endLonLat;
            String queryResult = getResponse(queryUrl);
            JSONObject jo = JSONObject.parseObject(queryResult);
            JSONArray ja = jo.getJSONArray("results");
            Object obj = JSONObject.parseObject(ja.getString(0)).get("distance");
            if (obj == null) {
                return result;
            }
            result = Long.parseLong(obj.toString());
            return result;
        }
    
        /**
         * 地理编码,根据地址获取经纬度
         *
         * @param address
         * @return
         */
        public static String getLonLat(String address) {
    
            String lonLat = "";
    
            // 高德地图地理编码API
            String queryUrl = String.format("https://restapi.amap.com/v3/geocode/geo?address=" + address + "&output=json&key=" + key);
    
            String queryResult = getResponse(queryUrl);
            JSONObject jo = JSONObject.parseObject(queryResult);
            JSONArray ja = jo.getJSONArray("geocodes");
            Object obj = JSONObject.parseObject(ja.getString(0)).get("location");
            if (obj == null) {
                return lonLat;
            }
            lonLat = obj.toString();
            return lonLat;
    
        }
    
    
        /**
         * 逆地理编码,根据经纬度获取地址
         *
         * @param longitude
         * @param latitude
         * @return
         */
        public static String getAddress(double longitude, double latitude) {
    
            String address = "";
    
            // 高德地图地理编码API
            String queryUrl = String.format("https://restapi.amap.com/v3/geocode/regeo?output=json&key=" + key + "&radius=1000&extensions=base&location=%s,%s", longitude, latitude);
    
            String queryResult = getResponse(queryUrl);
            JSONObject jo = JSONObject.parseObject(queryResult);
            JSONObject ja = jo.getJSONObject("regeocode");
            String obj = ja.getString("formatted_address");
            if (obj == null) {
                return address;
            }
            address = obj;
            return address;
    
        }
    
    
        /**
         * 用JAVA发起http请求,并返回json格式的结果
         * @param serverUrl
         * @return
         */
        private static String getResponse(String serverUrl) {
            //用JAVA发起http请求,并返回json格式的结果
            StringBuffer result = new StringBuffer();
            try {
                URL url = new URL(serverUrl);
                URLConnection conn = url.openConnection();
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
    
                String line;
                while ((line = in.readLine()) != null) {
                    result.append(line);
                }
                in.close();
    
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return result.toString();
        }
    
        public static void main(String[] args) {
    
    
            //获取地址1的经纬度
            String lonlat = getLonLat("广东省深圳市龙华区观湖街道澜景路");
            System.out.println(lonlat);
    
            //获取地址2的经纬度
            String lonlat1 = getLonLat("广东省深圳市龙华区观湖街道河南新村");
            System.out.println(lonlat1);
    
            //逆地理编码
            double longitude = Double.valueOf(lonlat.substring(0,lonlat.indexOf(",")));
            double latitude = Double.valueOf(lonlat.substring(lonlat.indexOf(",")+1,lonlat.length()));
    
            String gaodeResult = getAddress(longitude, latitude);
    
            System.out.println(gaodeResult);
    
            //比较两地间距离
            String startLonLat = lonlat;
            String endLonLat = lonlat1;
    
            Long dis = getDistance(startLonLat, endLonLat);
            System.out.println(dis);
    
    
    
        }
    
    
    }
  • 相关阅读:
    php实现上传图片保存到数据库的方法
    支付宝集成——如何在回调地址中使用自定义参数
    QQ音乐的各种相关API
    xampp默认mysql密码设置,修改mysql的默认空密码
    node.js 初体验
    php调用empty出现错误Can't use function return value in write context
    ecshop数据库操作函数
    ecshop中$user对象
    为什么我的联想打印机M7450F换完墨粉之后打印机显示请更换墨粉盒?这是我的墨盒第一次灌粉·、
    PHP中获取当前页面的完整URL
  • 原文地址:https://www.cnblogs.com/shoose/p/15241868.html
Copyright © 2011-2022 走看看