Как я могу отобразить сообщение в соответствии с дистанционным фильтром - PullRequest
0 голосов
/ 04 апреля 2019

Я создаю Post API в lumen framework, который содержит подробности пользователей и извлекает их текущую широту и долготу для каждого поста.Как я могу сделать API, где пост виден на определенном расстоянии (например, пост должен быть виден в пределах 10 км) для пользователей, присутствующих в этой области.Любое руководство приветствуется.

1 Ответ

0 голосов
/ 04 апреля 2019

Я не очень хорош в lumem, но я знаю, что это микро версия для laravel, поэтому я создал php-скрипт, который работает на the laravel OR PHP OR other PHP FRAMEWORKS

/**
    * @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);
...