地图相关

static final double EARTH_RADIUS = 6372.796924;
static final double PI = 3.1415926535898;

    /**
     * 根据中心坐标获取指定距离的随机坐标点
     *
     * @param center
     *            中心坐标
     * @param distance
     *            离中心坐标距离(单位:米)
     * @return 随机坐标
     */
    public static LatLng randomLocation(LatLng center, double distance) {
        if (distance <= 0) distance = 50;
        double lat, lon, brg, dist;
        double rad360 = 2 * PI;
        distance = distance / 1000;

        double maxDist = distance;
        maxDist = maxDist / EARTH_RADIUS;
        double startLat = rad(center.latitude);
        double startLon = rad(center.longitude);
        double cosDif = Math.cos(maxDist) - 1;
        double sinStartLat = Math.sin(startLat);
        double cosStartLat = Math.cos(startLat);
        dist = Math.acos((new Random().nextDouble() * cosDif + 1));
        brg = rad360 * new Random().nextDouble();
        lat = Math.asin(sinStartLat * Math.cos(dist) + cosStartLat * Math.sin(dist) * Math.cos(brg));
        lon = deg(normalizeLongitude(startLon * 1 + Math.atan2(Math.sin(brg) * Math.sin(dist) * cosStartLat,
                Math.cos(dist) - sinStartLat * Math.sin(lat))));
        lat = deg(lat);

        return new LatLng(padZeroRight(lat), padZeroRight(lon));
    }

    /**
     * 获取两点间的距离(单位:米)
     *
     * @param start
     *            起始坐标
     * @param end
     *            结束坐标
     * @return 距离
     */
    public static double GetDistance(LatLng start, LatLng end) {
        double radLat1 = rad(start.latitude);
        double radLat2 = rad(end.latitude);
        double a = radLat1 - radLat2;
        double b = rad(start.longitude) - rad(end.longitude);
        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 = (int) (s * 10000000) / 10000;
        return s;
    }

    /**
     * 弧度
     */
    static double rad(double d) {
        return d * PI / 180.0;
    }

    /**
     * 角度
     */
    static double deg(double rd) {
        return (rd * 180 / Math.PI);
    }

    static double normalizeLongitude(double lon) {
        double n = PI;
        if (lon > n) {
            lon = lon - 2 * n;
        } else if (lon < -n) {
            lon = lon + 2 * n;
        }
        return lon;
    }

    static double padZeroRight(double s) {
        double sigDigits = 8;
        s = Math.round(s * Math.pow(10, sigDigits)) / Math.pow(10, sigDigits);
        return s;
    }

你可能感兴趣的:(地图相关)