Elasticsearch-有没有一种方法可以将地理边界框与距离相结合

时间:2018-12-24 06:59:55

标签: elasticsearch-5

有没有一种方法可以将地理边界框({​​{3}})与距离结合起来。基本上我想在100 kms的地理边界框内获取数据。即top_left纬度/经度距中心纬度/经度50公里,而bottom_right则相同。

1 个答案:

答案 0 :(得分:0)

这帮助了。 https://zaemis.blogspot.com/2011/01/geolocation-search.html

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Geo location</title>
</head>
<body>
    <pre id="geolocation"></pre>
</body>
<script>

        const currentLocation = {
            lat: 10.7904833, //Tiruchirapalli, Tamilnadu
            lng : 78.7046725 //Tiruchirapalli, Tamilnadu
        }

        const distance = 31; // In miles (31 mile equal to 50km) 

        function degToRad(d){
            return d * (Math.PI/180);
        }

        function radToDeg(r){
            return r * (180/Math.PI);
        }

        function destination(lat,lon, bearing, distance) {
            const radius = 3963.19;
            const rLat = degToRad(lat);
            const rLon = degToRad(lon);
            const rBearing = degToRad(bearing);
            const rAngDist = distance / radius;

        const rLatB = Math.asin(Math.sin(rLat) * Math.cos(rAngDist) + Math.cos(rLat) * Math.sin(rAngDist) * Math.cos(rBearing));

        const rLonB = rLon + Math.atan2(Math.sin(rBearing) * Math.sin(rAngDist) * Math.cos(rLat), Math.cos(rAngDist) - Math.sin(rLat) * Math.sin(rLatB));

        return {
            lat : radToDeg(rLatB),
            lng : radToDeg(rLonB)
        }
    };

    /**
     * Call destination funtion
     * @param {latitude,longitude,degree,distance in miles} 
     */
    const boundaries = [{
        topLeft : destination(currentLocation.lat,currentLocation.lng,315,31),
        bottomRight : destination(currentLocation.lat,currentLocation.lng,135,31)
    }];

    document.getElementById("geolocation").innerHTML = JSON.stringify(boundaries[0],undefined,2);

    </script>
</html>