通过其他连接获得laravel 5中的纬度和经度
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过其他连接获得laravel 5中的纬度和经度相关的知识,希望对你有一定的参考价值。
这对我来说非常复杂。我有一个搜索路线 - 效果很好,除了我无法通过邮政编码搜索到最近的lat和一个给定的邮政编码。也就是说,我可以计算出lat和long,但我不确定如何将它集成到我现有的查询中。此查询是没有邮政编码的搜索查询:
$query = DB::table('dogs');
$query->leftJoin('dog_addresses','dogs.id','=','dog_addresses.dog_id');
$query->leftJoin('dog_videos','dogs.id','=','dogs_videos.dog_id');
$query->leftJoin('dogs_breeds','dogs.breed_id','=','dogs_breeds.id');
if($request->input("breed") && $request->input("breed") != "" && $request->input("breed") != "any")
{
$breed = Dog_Breed::where("breed_name", $request->input("breed"))->first();
$query->where('dogs.breed_id', $breed->id);
}
$results = $query->get();
我有一些东西要添加到查询中以获取邮政编码的纬度和经度:
if($request->input("postcode"))
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, "http://api.postcodes.io/postcodes/" . $request->input('postcode'));
$result = json_decode(curl_exec($curl));
curl_close($curl);
$postcode_lat = $result->result->latitude;
$postcode_long = $result->result->longitude;
}
这让我得到了我的邮政编码和经度。但是我不知道如何根据dog_addresses表中存在的lat和long列来获取狗的位置,该表连接到dog表。我该怎么做呢?
所以如果我的dog_addresses表有列Lat和Long。
所以狗:
id | user_id | dog_name | age
dog_addresses:
id | dog_id | address_line_1 | town | postcode | lat | long
所以对于我的查询,我需要得到所有的狗,其中繁殖的ID是1,但我想内部加入视频,所以我可以获得所有视频信息和地址信息,但我也想要排序的狗的顺序,他们有多接近是我输入的邮政编码,基于lat和long。
我很困惑。我找到了这个:
( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance
但我仍然不确定如何整合它,或者它对我有什么用处。请帮忙
答案
下面的代码将能够以您想要的方式检索结果。代码说明在线。
if($request->input("postcode"))
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, "http://api.postcodes.io/postcodes/" . $request->input('postcode'));
$result = json_decode(curl_exec($curl));
curl_close($curl);
$postcode_lat = $result->result->latitude;
$postcode_long = $result->result->longitude;
$query = DB::table('dogs');
//Join statement responsible for retrieving dogs addresses based on latitude and longitude in address table.
$query->join(DB::raw('(SELECT dog_id, (
3959 * acos (
cos ( radians($postcode_lat) )
* cos( radians( lat ) )
* cos( radians( long ) - radians($postcode_long) )
+ sin ( radians($postcode_lat) )
* sin( radians( lat ) )
)
)AS distance from dog_addresses) as dog_addresses'), function ($join){
$join->on('dogs.id', '=', 'dog_addresses.dog_id')
});
$query->leftJoin('dog_videos','dogs.id','=','dogs_videos.dog_id');
$query->leftJoin('dogs_breeds','dogs.breed_id','=','dogs_breeds.id');
if($request->input("breed") && $request->input("breed") != "" && $request->input("breed") != "any")
{
$breed = Dog_Breed::where("breed_name", $request->input("breed"))->first();
$query->where('dogs.breed_id', $breed->id);
}
$results = $query->orderBy('dog_addresses.distance', 'ASC') //Ordering the results in ascending order by calculated distance
->limit(20) //Limiting the results to 20 . Can be changed or removed according to your needs
->get(); //Retrieving the results
}
另一答案
你可以尝试mysql的GLength函数:
SELECT
*, GLength(LineString(GeomFromText('POINT(latPosCode,lngPosCode)'),GeomFromText('POINT(latInDataBase,lngInDataBase)'),)) AS DISTANCE
FROM dogs
ORDER BY DISTANCE
以上是关于通过其他连接获得laravel 5中的纬度和经度的主要内容,如果未能解决你的问题,请参考以下文章
通过给定的经度和纬度查找在数据库中注册的位置 - Laravel