根据经纬度计算距离(腾讯)

发布时间 2023-06-16 14:41:11作者: 不想挨捶的牛

1、根据经纬度计算距离

private static double EARTH_RADIUS = 6378.137;
private double rad(double d) {
    return d * Math.PI / 180.0;
}

/**
* @param targetLongitude  目标经度
* @param targetLatitude
* @param currentLongitude 当前经度
* @param currentLatitude
* @return
*/
private double range(double targetLongitude, double targetLatitude, double currentLongitude, double currentLatitude) {
    double radLat1 = rad(targetLatitude);
    double radLat2 = rad(currentLatitude);
    double a = radLat1 - radLat2;
    double b = rad(targetLongitude) - rad(currentLongitude);
    double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                                       + Math.cos(radLat1) * Math.cos(radLat2)
                                       * Math.pow(Math.sin(b / 2), 2)));
    s = s * EARTH_RADIUS;
    s = Math.round(s * 10000d) / 10000d;
    s = s * 1000;
    return s;
}
/**
* 逆地址
*
* @param currentLongitude--当前经度
* @param currentLatitude--当前纬度
* @return
*/
private String TxServiceGcoder(double currentLongitude, double currentLatitude) {
    String address = "";
    String url = "https://apis.map.qq.com/ws/geocoder/v1/";
    String key = "";
    String param = "location=" + currentLatitude + "," + currentLongitude + "&key=" + key;
    String result = HttpRequest.sendGet(url, param);
    JSONObject json = JSON.parseObject(result);
    int status = (int) json.get("status");
    if (status == 0) {
        address = json.getJSONObject("result").get("address").toString();
    } else {
        throw new RuntimeException(result);
    }
    return address;
}