我正在尝试通过距离从mysql数据库中检索位置,我需要一个查询,在那里我可以插入一个点,并从我的数据库中获得10英里内的点,关于查询时间和性能我有这个代码在线,但它不工作,你有什么建议
set @lat= 0;
set @lon = 0;
set @dist = 10;
set @rlon1 = @lon-@dist/abs(cos(radians(@lat))*69);
set @rlon2 = @lon+@dist/abs(cos(radians(@lat))*69);
set @rlat1 = @lat-(@dist/69);
set @rlat2 = @lat+(@dist/69);
select * from points
where st_within(4326, envelope(linestring(point(@rlon1, @rlat1), point(@rlon2, @rlat2))))
order by st_distance(point( 0,0), 4326) limit 10;发布于 2014-07-31 17:43:38
您找到的代码是用于带有PostGIS插件的PostgreSQL。它在MySQL中不起作用。
对于地理空间查询,最好将PostgreSQL与PostGIS插件一起使用,但如果您所需要的只是给定坐标10英里范围内的坐标列表,那么MySQL就可以了。
发布于 2014-07-31 18:35:26
使用this blog post中的函数可以计算搜索半径的边界框。这只是一个近似值,会返回一些比所需距离稍远的结果。
$coordinate = array
( $latitude,
$longitude,
);
$distanceInKilometres = ($distanceInMiles * 1.609344);
$boundBoxTop = distanceInKilometres($coordinate, $distanceInKilometres, 0);
$boundBoxRight = distanceInKilometres($coordinate, $distanceInKilometres, 90);
$boundBoxBottom = distanceInKilometres($coordinate, $distanceInKilometres, 180);
$boundBoxLeft = distanceInKilometres($coordinate, $distanceInKilometres, 270);
$sql = '
SELECT
*,
SQRT(POWER(ABS(`latitude` - '.$latitude.'), 2) + POWER(ABS(`longitude` - '.$longitude.'), 2)) AS `distance`
FROM
`points`
WHERE
`latitude` >= '.$boundBoxTopBottom[0].'
AND `longitude` >= '.$boundBoxTopLeft[1].'
AND `latitude` <= '.$boundBoxTopTop[0].'
AND `longitude` <= '.$boundBoxTopRight[1].'
ORDER BY
`distance`
';
// run the query and get the results假设您有变量$latitude、$longitude和$distanceInMiles。
https://stackoverflow.com/questions/25055415
复制相似问题