0

A という GPS 座標があるとします (緯度経度の 10 進形式で、半径 30 メートルのゾーン (円) を作成して、そのゾーンに入る (GPS 座標を持つ) デバイスをトラップできるようにする方法)。ありがとう

4

1 に答える 1

2

この JavaScript の例を見てください。他の言語でも簡単に実装できます。

メインコード:

    var earthRadiusKm = 3437.74677 * 1.1508 * 1.6093470878864446;

    function GeoArea(centerLat, centerLng, distanceKm, northPoint, southPoint, eastPoint, westPoint) {
        this.northPoint = northPoint;
        this.southPoint = southPoint;
        this.eastPoint = eastPoint;
        this.westPoint = westPoint;
        this.inArea = function (lat, lng) {
            var inBox = southPoint.lat <= lat && lat <= northPoint.lat && westPoint.lng <= lng && lng <= eastPoint.lng;
            if (inBox) {
                var distanceFromCenterKm = calcDistance(centerLat, centerLng, lat, lng);
                return distanceFromCenterKm <= distanceKm;
            } else {
                return false;
            }
        }
    }

    function GeoPoint(lat, lng) {
        this.lat = lat;
        this.lng = lng;
    }

    function toDegrees(radians) {
        return radians / (Math.PI / 180);
    }

    function toRadians(degrees) {
        return Math.PI / 180 * degrees;
    }

    function calcDistance(latA, lngA, latB, lngB) {
        var rLatA = toRadians(latA);
        var rLatB = toRadians(latB);
        var rHalfDeltaLat = toRadians((latB - latA) / 2);
        var rHalfDeltaLng = toRadians((lngB - lngA) / 2);

        return 2 * earthRadiusKm * Math.asin(Math.sqrt(Math.pow(Math.sin(rHalfDeltaLat), 2) + Math.cos(rLatA) * Math.cos(rLatB) * Math.pow(Math.sin(rHalfDeltaLng), 2)));
    }

    function findPoint(lat, lng, bearing, distance) {
        var rLat = toRadians(lat);
        var rLng = toRadians(lng);
        var rBearing = toRadians(bearing);
        var rAngDist = distance / earthRadiusKm;

        var rLatB = Math.asin(Math.sin(rLat) * Math.cos(rAngDist) + Math.cos(rLat) * Math.sin(rAngDist) * Math.cos(rBearing));
        var rLngB = rLng + Math.atan2(Math.sin(rBearing) * Math.sin(rAngDist) * Math.cos(rLat), Math.cos(rAngDist) - Math.sin(rLat) * Math.sin(rLatB));

        var pLat = toDegrees(rLatB);
        var pLng = toDegrees(rLngB);
        return new GeoPoint(pLat, pLng);
    }

    function calcArea(lat, lng, distanceKm) {
        var northPoint = findPoint(lat, lng, 0, distanceKm);
        var eastPoint = findPoint(lat, lng, 90, distanceKm);
        var southPoint = findPoint(lat, lng, 180, distanceKm);
        var westPoint = findPoint(lat, lng, 270, distanceKm);
        return new GeoArea(lat, lng, distanceKm, northPoint, southPoint, eastPoint, westPoint);
    }

使用例:

    // calculate area with center in lat:55.742793 lng:37.615401
    // and distance in 23 km
    var area = calcArea(55.742793, 37.615401, 23);

    //returns true
    area.inArea(55.714735, 37.629547);
    //returns false
    area.inArea(55.693842, 38.015442);
于 2012-05-07T07:11:19.497 に答える