任何人都可以帮我解决这个问题。
我有一个Concert
模型和另一个Location
模型。音乐会属于一个位置。位置表有一个空间点字段。
我需要做三件事:
到目前为止,我已设法设置数据库并将音乐会与用户保持一定距离:
$condition = ($lat != null && $lon != null && $distance != null);
Concert::when($condition, function ($query) use ($distance,$lat,$lon) {
return $query->whereHas('location', function($query) use ($distance,$lat,$lon) {
$query->whereRaw('st_distance(location,POINT('.$lat.','.$lon.')) < '.$distance);
});
})
->get();
你能帮我看看如何在视图中显示这个st_distance
吗?
我如何按距离订购?
我在$distance
以度数给出的文档中读到以英里/公里为单位的st_distance
的正确方法是什么?
答案 0 :(得分:1)
您可以选择ST_DISTANCE
或ST_DISTANCE_SPHERE
(MySQL Spatial Convenience Functions)然后:
ORDER BY
sql clause HAVING
sql子句(why can't use WHERE clause here)$raw = 'SELECT
ST_X(location) AS longitude,
ST_Y(location) AS latitude,
ST_DISTANCE_SPHERE(location, POINT(?, ?)) AS dist
FROM
concerts,
locations
WHERE
concerts.id = locations.id
HAVING
dist < ?
ORDER BY
dist';
$pointsWithDist = DB::select($raw, [$lon, $lat, $distance]);
注意:
POINT
空间函数ST_DISTANCE
和ST_DISTANCE_SPHERE
默认以米返回距离您可以使用谷歌地图显示数据