php - 计算起点半径内位置的最佳方法

标签 php mysql postal-code haversine

我的目标是在我的最新项目中创建一个功能,最好使用 PHP。当每个用户注册时,他们将输入他们的邮政编码。然后希望我将使用 Open Street Map 将其转换为纬度/经度。

无论如何,我希望能够找到位于当前用户附近的其他用户。 我见过很多人使用 Haversine 公式,但是这意味着用户查询每个其他用户的详细信息来计算距离。我可以缓存它,但随着新用户注册,它很快就会过时。

运行以下查询会对我的系统产生什么样的影响?

sql = "SELECT zipcode, ( 3959 * acos( cos( radians( {$coords['latitude']} ) ) 
    * cos( radians( latitude ) ) * cos( radians( longitude ) 
    - radians( {$coords['longitude']} ) ) 
    + sin( radians( {$coords['latitude']} ) ) * sin( radians( latitude ) ) ) ) 
    AS distance FROM zipcodes HAVING distance <= {$radius} ORDER BY distance";

这是从某人的博客中提取的。

我没有关于注册率或用户数量的任何数据,因为它仍在开发中。

我将不胜感激任何反馈或我可以用来在特定半径内找到匹配用户的其他方法。

最佳答案

mySql 4.1 版中有 GIS 和空间扩展,请参阅 here .从描述中您会发现,它用于解决您在此处遇到的问题:

A GIS (geographic information system) stores and looks up objects which have one or more spatial attributes, such as size and position, and is used to process such objects. A simple example would be a system that stores addresses in a town using geographic coordinates. If this rather static data was then combined with other information, such as the location of a taxi-cab, then this data could be used to find the closest cab to a certain location.

它向 MySql 添加了一些东西,例如:

  • 空格键和 POINT 类型:

    创建表地址( 地址 CHAR(80) NOT NULL, address_loc POINT 不为空, 主键(地址), 空间 KEY(address_loc) );

  • 转换例程

    INSERT INTO 地址 VALUES('Foobar street 12', GeomFromText('POINT(2671 2500)'));

  • GIS计算功能

    选择 c.出租车司机, ROUND(GLength(LineStringFromWKB(LineString(AsBinary(c.cab_loc), AsBinary(a.address_loc))))) AS 距离 从驾驶室 c,地址 a 按距离排序 ASC LIMIT 1;

(示例取自上面的链接)

关于php - 计算起点半径内位置的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1193963/

相关文章:

php - 我需要清理数据库查询的结果吗?

php - JQuery 没有返回值

PHP 从 mysql 中提取每日平均值的平均值

mysql - NodeJS UTF8 字符串显示为 ??而不是表情符号

php - 在同一字段中搜索城市或邮政编码

php - Laravel 4.2 - 添加时间到时间戳

php和mysql插入输入数组

php - 动态构建受限逻辑处理器

postal-code - 格式化英国邮政编码以进行存储

python - 从列表中拆分邮政编码