如何通过Laravel 5中的地理位置进行查询? [英] How do I query by geolocation in Laravel 5?

查看:390
本文介绍了如何通过Laravel 5中的地理位置进行查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个地理位置属性到我的候选雄辩模型。我想根据他们的位置找到候选人。例如,在旧金山20英里内找到所有候选人。我的口才模型如何将候选人的位置存储在数据库中?



我如何根据他们的位置查询候选人?我正在使用Laravel 5.3。

解决方案

个人而言,我已经通过存储一组纬度和经度坐标实现了这一点。 p>

您可以使用 Haversine公式计算乌鸦飞行距离指定的一组坐标和您的情况下的候选人集合。



这是一个这样的查询的示例。例如,计算距离37,-122坐标25英里内最近的20个位置。

  SELECT id,(3959 * acos(cos(弧度(37))* cos(弧度(lat))
* cos(弧度(lng) - 弧度(-122))+ sin(弧度(37) )* sin(弧度(纬度))))AS距离
FROM标记
HAVING距离< 25
ORDER BY distance
LIMIT 0,20;

最初发现这里



这可以很容易地适应你的需要,如果你要添加lat / lng您的候选模式。

  SELECT *,(3959 * acos(cos(弧度(37))* cos(弧度(lat) )
* cos(弧度(lng) - 弧度(-122))+ sin(弧度(37))* sin(弧度(纬度))))AS距离
从候选人
ORDER BY distance
LIMIT 0,20;

您可以将其实现为某种查询范围,例如(未测试,输入消毒量最小)

  public function scopeClosestTo($ query,$ lat,$ lng)
{
return $ query-> orderByRaw(
'(3959 * acos(cos(radians('。floatval($ lat)。'))* cos(弧度(lat))* cos (lng) - 弧度('。floatval($ lng)'))+ sin(弧度('。intval($ lat)。'))* sin(弧度(lat))))ASC'
) ;
}

注意: 3959在这种情况下,半径以英里计算,公式将计算英里的距离。如果您需要距离为公里,请将3959更改为6371.感谢@Lionel在评论中指出这一点。


I am looking to add a geolocation attribute to my "Candidate" Eloquent model. I want to find Candidates based on their location. For instance, find all Candidates within 20 miles of San Francisco. What should my Eloquent model look like to store a Candidate's location in the database?

How do I query for Candidates based on their location? I am using Laravel 5.3.

解决方案

Personally, I have achieved this previously by storing a set of latitude and longitude coordinates.

You can use something called the Haversine formula to calculate "as the crow flies" distance between a specified set of coordinates and in your case the set of candidates.

Here's an example of such a query. This one, for instance calculates "the closest 20 locations that are within a radius of 25 miles to the 37, -122 coordinate."

SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) 
* cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin(radians(lat)) ) ) AS distance 
FROM markers 
HAVING distance < 25 
ORDER BY distance 
LIMIT 0 , 20;

Originally found here.

This could quite easily be adapted to suit your needs, if you were to add lat/lng to your Candidate model.

SELECT *, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) 
* cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin(radians(lat)) ) ) AS distance
FROM candidates
ORDER BY distance 
LIMIT 0, 20;

You could probably implement this as some kind of query scope, e.g. (Not tested and has minimal input sanitization)

public function scopeClosestTo($query, $lat, $lng)
{
    return $query->orderByRaw(
        '(3959 * acos(cos(radians(' . floatval($lat) . ')) * cos(radians(lat)) * cos(radians(lng) - radians(' . floatval($lng) . ')) + sin(radians(' . intval($lat) . ')) * sin(radians(lat)))) ASC'
    );
}

Note: 3959 in this case is the Earth's radius in miles and the formula will calculate distances in miles. If you need distances to be kilometers, change 3959 to 6371. Thanks to @Lionel for pointing this out in the comments.

这篇关于如何通过Laravel 5中的地理位置进行查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆