之前一直使用 Redis 的 GeoHash 来查询附近的目标,但是为了架构的简化,我决定尝试一下 MySQL 来进行查询。
SELECT name, latitude, longitude, SQRT(
POW(69.1 * (latitude - [target_lat]), 2) +
POW(69.1 * ([target_lng] - longitude) * COS(latitude / 57.3), 2)) AS distance
FROM TableName
HAVING distance < [distance]
ORDER BY distance;
单位为 miles, 英里。
1 英里=1.6093 千米
参考
https://stackoverflow.com/questions/2234204/latitude-longitude-find-nearest-latitude-longitude-complex-sql-or-complex-calc