Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 有没有办法找到拉维尔半径内的位置_Php_Laravel 5_Geolocation - Fatal编程技术网

Php 有没有办法找到拉维尔半径内的位置

Php 有没有办法找到拉维尔半径内的位置,php,laravel-5,geolocation,Php,Laravel 5,Geolocation,如何根据一点经纬度和给定半径找到基于radious的位置。 假设,我给了30作为半径,它会找到特定给定纬度和经度的结果,半径为30。 我尝试了以下代码: $lat = '37.421998'; // latitude of centre of bounding circle in degrees $lon = '-122.084000'; // longitude of centre of bounding circle in degrees $rad = 30; //

如何根据一点经纬度和给定半径找到基于radious的位置。 假设,我给了30作为半径,它会找到特定给定纬度和经度的结果,半径为30。 我尝试了以下代码:

    $lat = '37.421998'; // latitude of centre of bounding circle in degrees
    $lon = '-122.084000'; // longitude of centre of bounding circle in degrees
    $rad = 30; // radius of bounding circle in kilometers

    $R = 6371;  // earth's mean radius, km

    // first-cut bounding box (in degrees)
    $maxLat = $lat + rad2deg($rad/$R);
    $minLat = $lat - rad2deg($rad/$R);
    $maxLon = $lon + rad2deg(asin($rad/$R) / cos(deg2rad($lat)));
    $minLon = $lon - rad2deg(asin($rad/$R) / cos(deg2rad($lat)));

   $results = DB::select('Select id, latitude, longitude, acos(sin(:lat)*sin(radians(latitude)) + cos(:lat)*cos(radians(latitude))*cos(radians(longitude)-(:lon))) * (:R) As D From ( Select id, latitude, longitude From jobs_master Where latitude Between (:minLat) And (:maxLat) And longitude Between (:minLon) And (:maxLon) ) As FirstCut Where acos(sin(:lat)*sin(radians(latitude)) + cos(:lat)*cos( radians(latitude))*cos(radians(longitude)-(:lon))) * :R < :rad ', ['maxLat' => $maxLat,'maxLon' => $maxLon, 'minLat' => $minLat,'minLon' => $minLon, 'lat' => $lat,'lon' => $lon,'R'=> $R,'rad'=>$rad]);

        dd(DB::getQueryLog());exit;
        print_r($results);exit;

任何人都可以帮助我。

您可以使用以下计算并根据需要修改查询:

$query = "SELECT (3956 * 2 * ASIN(SQRT( POWER(SIN(( $latitude - latitude) *  pi()/180 / 2), 2) + COS( $latitude * pi()/180) * COS(latitude * pi()/180) * POWER(SIN(( $longitude - longitude) * pi()/180 / 2), 2) ))) as distance, latitude, longitude" FROM TABLES HAVING distance <= 30
因此,在上面的查询中,我们首先根据给定的纬度和经度计算距离,然后使用HAVING子句by将距离限制为30公里半径


希望这能解决您的问题:

您可以使用以下计算并根据需要修改查询:

$query = "SELECT (3956 * 2 * ASIN(SQRT( POWER(SIN(( $latitude - latitude) *  pi()/180 / 2), 2) + COS( $latitude * pi()/180) * COS(latitude * pi()/180) * POWER(SIN(( $longitude - longitude) * pi()/180 / 2), 2) ))) as distance, latitude, longitude" FROM TABLES HAVING distance <= 30
因此,在上面的查询中,我们首先根据给定的纬度和经度计算距离,然后使用HAVING子句by将距离限制为30公里半径


希望这能解决您的问题:

这里有一个很好的解决方案,可以使用雄辩的查询来解决您与Laravel之间的问题,此方法适用于不同的场景,但正是您想要的:

private function findNearestRestaurants($latitude, $longitude, $radius = 400)
    {
        /*
         * using eloquent approach, make sure to replace the "Restaurant" with your actual model name
         * replace 6371000 with 6371 for kilometer and 3956 for miles
         */
        $restaurants = Restaurant::selectRaw("id, name, address, latitude, longitude, rating, zone ,
                         ( 6371000 * acos( cos( radians(?) ) *
                           cos( radians( latitude ) )
                           * cos( radians( longitude ) - radians(?)
                           ) + sin( radians(?) ) *
                           sin( radians( latitude ) ) )
                         ) AS distance", [$latitude, $longitude, $latitude])
            ->where('active', '=', 1)
            ->having("distance", "<", $radius)
            ->orderBy("distance",'asc')
            ->offset(0)
            ->limit(20)
            ->get();

        return $restaurants;
    }

参考资料:

这里有一个很好的解决方案,可用于解决您在使用Laravel时遇到的问题,该方法适用于不同的场景,但正是您需要的:

private function findNearestRestaurants($latitude, $longitude, $radius = 400)
    {
        /*
         * using eloquent approach, make sure to replace the "Restaurant" with your actual model name
         * replace 6371000 with 6371 for kilometer and 3956 for miles
         */
        $restaurants = Restaurant::selectRaw("id, name, address, latitude, longitude, rating, zone ,
                         ( 6371000 * acos( cos( radians(?) ) *
                           cos( radians( latitude ) )
                           * cos( radians( longitude ) - radians(?)
                           ) + sin( radians(?) ) *
                           sin( radians( latitude ) ) )
                         ) AS distance", [$latitude, $longitude, $latitude])
            ->where('active', '=', 1)
            ->having("distance", "<", $radius)
            ->orderBy("distance",'asc')
            ->offset(0)
            ->limit(20)
            ->get();

        return $restaurants;
    }

参考资料:

我没有将距离字段存储到数据库中。在表中,纬度和经度仅在那里。请告诉我上面的答案中的3956是什么,我将尝试使用它?感谢它在sql环境中对我有效,但在laravel结构中它不起作用!。。您的表中不需要距离字段。这里的距离实际上是sql中执行的计算的别名。您始终可以使用DB facade(即查询生成器)在laravel中编写手动查询。仅供参考:我并没有将距离字段存储到数据库中。在表中,纬度和经度仅在那里。请告诉我上面的答案中的3956是什么,我将尝试使用它?感谢它在sql环境中对我有效,但在laravel结构中它不起作用!。。您的表中不需要距离字段。这里的距离实际上是sql中执行的计算的别名。您始终可以使用DB facade(即查询生成器)在laravel中编写手动查询。供参考: