【问题标题】:How can i display post according to distance filter如何根据距离过滤器显示帖子
【发布时间】:2019-08-25 17:01:45
【问题描述】:

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

【问题讨论】:

    标签: php laravel rest lumen


    【解决方案1】:

    我不太擅长 lumem,但我知道它是 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 =&gt; which indicates the latitude of point one

    $longitudeOne =&gt; which indicates the longitude of point one

    $latitudeTwo =&gt; which indicates the latitude of point two

    $longitudeTwo =&gt; which indicates the longitude of point two

    $distanceUnit =&gt; sets that in which unit the distance needs to be calculated

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

    默认为 KM

    $round =&gt; 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);
    

    【讨论】:

    • 感谢您的指导好友,但仍然有可能假设我创建了一个帖子并且我希望该帖子对该特定区域内的所有用户可见。据我了解,此代码查找两个指定点之间的距离
    • 是的,您可以根据 IP 地址ipstack.com 获取当前用户的纬度和经度,然后您可以将这些值传递给我的函数
    • 我的意思是我将所有用户的纬度和经度都存储在我的用户表中,并且我还有另一个名为 post 的表。当个人创建帖子时,它将在创建帖子时捕获纬度和经度并将其发送到帖子表。我希望我的帖子的表格 api 在用户表格中查找 10 公里的距离并向他们展示该帖子
    • 你能分享一些数据吗
    • public function up() { Schema::connection('mysql1')->create('posts', function (Blueprint $table) { $table->bigIncrements('id'); $ table->integer('post_id')->unique(10); $table->integer('user_id'); $table->string('location'); $table->string('title'); $ table->string('description'); $table->float('lat'); $table->float('lon'); $table->timestamps(); }); }
    猜你喜欢
    • 1970-01-01
    • 2021-11-22
    • 2019-04-27
    • 1970-01-01
    • 2015-06-27
    • 1970-01-01
    • 2020-11-14
    • 2017-06-21
    • 1970-01-01
    相关资源
    最近更新 更多