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);



    }


}

 

posted @ 2021-09-08 11:09  shuzu渊  阅读(752)  评论(0编辑  收藏  举报