首页 归档 关于 learn love 工具

地理位置计算

Java 获取指定范围内的经纬度, 用的是正方形范围,而不是圆形


import javax.validation.constraints.NotNull;

/**
 * 经纬度信息
 */
public class GeoService {

    /**
     * 当前定位的纬度
     */
    Double latitude;
    // 当前定位经度
    Double longitude;

    /**
     * 每米的经度
     */
    private final double longitudeMi = 0.00001141;
    /**
     * 每米的纬度
     */
    private final double latitudeMi = 0.00000899;


    private static final double EARTH_RADIUS = 6378137;//赤道半径

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

    /**
     * 获取当前定位与另一个坐标的距离
     *
     * @param latitude
     * @param longitude
     * @return
     */
    public double getDistance(double latitude, double longitude) {
        if (this.latitude <=0 ){
            return 0;
        }
        double radLat1 = rad(this.latitude);
        double radLat2 = rad(latitude);
        double a = radLat1 - radLat2;
        double b = rad(this.longitude) - rad(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;
        return s;//单位 米
    }


    public GeoService(@NotNull double latitude, @NotNull double longitude) {
        this.latitude = latitude;
        this.longitude = longitude;
    }

    /**
     * 获取指定范围的经度左偏移量
     *
     * @param distance
     * @return
     */
    public double getLongitudeLeft(int distance) {
        return longitude - this.longitudeMi * distance;
    }

    /**
     * 获取指定范围的经度右偏移量
     *
     * @param distance
     * @return
     */
    public double getLongitudeRight(int distance) {
        return longitude + this.longitudeMi * distance;
    }
    /**
     * 获取指定范围的纬度左偏移量
     *
     * @param distance
     * @return
     */
    public double getLatitudeLeft(int distance) {
        return latitude - this.latitudeMi * distance;
    }

    public double getLatitudeRight(int distance) {
        return latitude + this.latitudeMi * distance;
    }

}

javascript 腾讯地图转百度地图坐标

/**
* 坐标转换,百度地图坐标转换成腾讯地图坐标
* lng 腾讯经度(pointy)
* lat 腾讯纬度(pointx)
* 经度>纬度
*/
function bMapToQQMap(lng, lat) {

    if (lng == null || lng == '' || lat == null || lat == '')
        return [lng, lat];

    var x_pi = 3.14159265358979324;
    var x = parseFloat(lng) - 0.0065;
    var y = parseFloat(lat) - 0.006;
    var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
    var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
    var lng = (z * Math.cos(theta)).toFixed(7);
    var lat = (z * Math.sin(theta)).toFixed(7);

    return [lng, lat];

}

/**
* 坐标转换,腾讯地图转换成百度地图坐标
* lng 腾讯经度(pointy)
* lat 腾讯纬度(pointx)
* 经度>纬度
*/

function qqMapToBMap(lng, lat) {

    if (lng == null || lng == '' || lat == null || lat == '')
        return [lng, lat];

    var x_pi = 3.14159265358979324;
    var x = parseFloat(lng);
    var y = parseFloat(lat);
    var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
    var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
    var lng = (z * Math.cos(theta) + 0.0065).toFixed(5);
    var lat = (z * Math.sin(theta) + 0.006).toFixed(5);
    return [lng, lat];

}

参考
https://www.cnblogs.com/zyulike/p/11812534.html