Php 如何根据距离过滤器显示帖子
我正在lumen框架中创建PostAPI,其中包含用户详细信息,并获取每个帖子的当前纬度和经度。Php 如何根据距离过滤器显示帖子,php,laravel,rest,lumen,Php,Laravel,Rest,Lumen,我正在lumen框架中创建PostAPI,其中包含用户详细信息,并获取每个帖子的当前纬度和经度。 我如何制作一个API,在这个API中,用户可以在特定距离内看到帖子(比如,帖子应该在10公里内可见)。感谢您的指导。我对lumem不太在行,但我知道它是laravel的微型版本,所以我创建了php脚本,可以在laravel或php或其他php框架上运行 /** * @function calculateDistanceBetweenTwoPoints * @author
我如何制作一个API,在这个API中,用户可以在特定距离内看到帖子(比如,帖子应该在10公里内可见)。感谢您的指导。我对lumem不太在行,但我知道它是laravel的微型版本,所以我创建了php脚本,可以在laravel或php或其他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;
}
}
因此,在您的情况下,可以按如下方式传递参数
echo calculateDistanceBetweenTwoPoints('11.657740','77.766270','11.074820','77.002160','KM',false,0);
谢谢你的指导,但是假设我创建了一个帖子,并且我希望该帖子对该特定区域内的所有用户都可见,这仍然是可能的。据我所知,这段代码查找两个指定点之间的距离。您可以根据ip地址获取当前用户的纬度和经度,并将这些值传递给我的函数。我的意思是,我将所有用户的纬度和经度存储在我的用户表中,我还有另一个表名为post。当一个人创建一个post时,它将在创建post时捕获lat和lon并将其发送到post表。我希望我的帖子的table api在users表中查找10km距离,并向他们显示postcan您可以共享一些datapublic函数up(){Schema::connection('mysql1')->create('posts',function(Blueprint$table){$table->bigmentrements('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();})
echo calculateDistanceBetweenTwoPoints('11.657740','77.766270','11.074820','77.002160','KM',false,0);