如何根据距离过滤器显示帖子

时间:2019-04-04 03:40:01

标签: php laravel rest lumen

我正在lumen框架中创建Post API,该API包含用户详细信息,并获取每个帖子的当前经度和纬度。 我如何制作API,使该区域内的用户可以在特定距离内看到该帖子(例如该帖子应该在10公里之内可见)。任何指导表示赞赏。

1 个答案:

答案 0 :(得分:0)

我的手法不佳,但是我知道它是laravel的微型版本,因此我创建了可以在 the laravel OR PHP OR other PHP FRAMEWORKS

上使用的php脚本
/**
    * @function     calculateDistanceBetweenTwoPoints
    * @author       Manojkiran <manojkiran10031998@gmail.com>
    * @param        string  $latitudeOne
    * @param        string  $longitudeOne
    * @param        string  $latitudeTwo
    * @param        string  $longitudeTwo
    * @param        string  $distanceUnit
    * @param        boolean $round
    * @param        string  $decimalPoints
    * @usage        calculates the distance between  latitude and longitude coordiates
    * @version      1.3
    **/
    /*
    |--------------------------------------------------------------------------
    | calculates the distance between  latitude and longitude coordiates by different distance units such as miles,kilometes etc
    |--------------------------------------------------------------------------
    |
    |
    |Usage:
    |Option 1: Returning the round value of the distance
    |echo calculateDistanceBetweenTwoPoints('11.657740','77.766270','11.074820','77.002160','ML',true);
    |
    |Option 2: Returning the Distance with specific decimal points
    |echo calculateDistanceBetweenTwoPoints('11.657740','77.766270','11.074820','77.002160','ML',false,2);
    |echo calculateDistanceBetweenTwoPoints('11.657740','77.766270','11.074820','77.002160','ML',false,7);
    |
    */

    function calculateDistanceBetweenTwoPoints(float $latitudeOne, float $longitudeOne, float $latitudeTwo , float $longitudeTwo,string  $distanceUnit ='KM', bool $round = false , int $decimalPoints = 3)
    {

        $distanceUnit = strtolower($distanceUnit);
        $pointDifference = $longitudeOne - $longitudeTwo;
        $toSin = (sin(deg2rad($latitudeOne)) * sin(deg2rad($latitudeTwo))) + (cos(deg2rad($latitudeOne)) * cos(deg2rad($latitudeTwo)) * cos(deg2rad($pointDifference)));
        $toAcos = acos($toSin);
        $toRad2Deg = rad2deg($toAcos);

        $toMiles  =  $toRad2Deg * 60 * 1.1515;
        $toKilometers = $toMiles * 1.609344;
        $toNauticalMiles = $toMiles * 0.8684;
        $toMeters = $toKilometers * 1000;
        $toFeets = $toMiles * 5280;
        $toYards = $toFeets / 3;


              switch (strtoupper($distanceUnit)) 
              {
                  case 'ML'://miles
                         $toMiles  = ($round == true ? round($toMiles) : round($toMiles, $decimalPoints));
                         return $toMiles;
                      break;
                  case 'KM'://Kilometers
                        $toKilometers  = ($round == true ? round($toKilometers) : round($toKilometers, $decimalPoints));
                        return $toKilometers;
                      break;
                  case 'MT'://Meters
                        $toMeters  = ($round == true ? round($toMeters) : round($toMeters, $decimalPoints));
                        return $toMeters;
                      break;
                  case 'FT'://feets
                        $toFeets  = ($round == true ? round($toFeets) : round($toFeets, $decimalPoints));
                        return $toFeets;
                      break;
                  case 'YD'://yards
                        $toYards  = ($round == true ? round($toYards) : round($toYards, $decimalPoints));
                        return $toYards;
                      break;
                  case 'NM'://Nautical miles
                        $toNauticalMiles  = ($round == true ? round($toNauticalMiles) : round($toNauticalMiles, $decimalPoints));
                        return $toNauticalMiles;
                      break;
            }             
    }
  

说明

参数

$latitudeOne => which indicates the latitude of point one

$longitudeOne => which indicates the longitude of point one

$latitudeTwo => which indicates the latitude of point two

$longitudeTwo => which indicates the longitude of point two

$distanceUnit => sets that in which unit the distance needs to be calculated

AVAILABLE UNITS[ML => MILE(S),KM => KILOMETER(S),MT => METER(S),FT => FEET(S),YD => YARD(S),NM => NAUTICAL MILE(S)]

默认为 KM

$round => Set to true if you want to round of the value

$decimalPoints => Set the number of digits that need to be added for the float values

因此,在您遇到的情况下,您可以按以下方式传递参数

echo calculateDistanceBetweenTwoPoints('11.657740','77.766270','11.074820','77.002160','KM',false,0);