php - 地理距离搜索 MYSQL

标签 php mysql geolocation coordinates spatial

我正在使用 mysql 使用空间扩展进行地理搜索。为此,我做了一个存储过程(当我像那样在 mysql 中尝试命令行时,它工作得非常好

mysql> CALL test2(GeomFromText('POINT(-0.93961472 43.52843475)'),0.6 );

DROP PROCEDURE `test2`//
CREATE DEFINER=`root`@`localhost` PROCEDURE `test2`(IN center2 point,IN dist int)
BEGIN
SET @center = center2;
SET @radius = dist;

SET @bbox = CONCAT('POLYGON((', 
 X(@center) - @radius, ' ', Y(@center) - @radius, ',', 
 X(@center) + @radius, ' ', Y(@center) - @radius, ',', 
 X(@center) + @radius, ' ', Y(@center) + @radius, ',', 
 X(@center) - @radius, ' ', Y(@center) + @radius, ',', 
 X(@center) - @radius, ' ', Y(@center) - @radius, '))' 
);

SELECT professionnels.*, AsText(coord) 
FROM professionnels 
WHERE Intersects( coord, GeomFromText(@bbox) ) 
AND SQRT(POW( ABS( X(coord) - X(@center)), 2) + POW( ABS(Y(coord) - Y(@center)), 2 )) < @radius limit 20;
end

但问题是我需要从 php 调用它。当我这样做时,返回一个错误说

PROCEDURE test2 can't return a result set in the given context

那么..我如何使用转身返回一组数据?

谢谢

最佳答案

你可以把最后一部分改成

/*delete all rows from temp_table*/
DELETE FROM temp_table WHERE temp_table.somefield is not null;

INSERT INTO temp_table
  SELECT professionnels.*, AsText(coord) 
  FROM professionnels 
  WHERE Intersects( coord, GeomFromText(@bbox) ) 
  AND SQRT(POW( ABS( X(coord) - X(@center)), 2) 
      + POW( ABS(Y(coord) - Y(@center)), 2 )) < @radius limit 20;

然后您可以从 temp_table 中进行选择并在那里找到结果集。

关于php - 地理距离搜索 MYSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6023961/

相关文章:

python - 导入错误: No module named geocoders

redis - 从Redis的源码看 "area"和 "BoundingBox"有什么区别

php - 如何让 PHP、Symlinks 和 __FILE__ 很好地协同工作?

javascript - ajax未到达php端

php - XML 到 JSON 或数组? PHP

mysql - 在 MySQL Workbench 中,使用 "Table Data Import Wizard"导入 CSV 创建空表

java - 查找距离我的位置 180 度(南)1 公里的位置

php - CodeIgniter 2.1.0 session 问题

php - 将 POST 后的表单值保留为 python 文件的参数

PHP - LocalHost数据库简单连接SQL