2012-02-02 42 views
1

我一直在研究如何最好地从MySQL数据库检索位置数据的子集。这个答案有联系许多人希望诱导的文章,但其中没有提供的任何具体,简单的例子:MySQL:简单的地理/空间查询

Database: Best performance way to query geo location data?


比方说,我有一个表放置一个数据库。地方可能有数​​百万条目。每行有列类型LAT,LON和NAME。

我想形成从以下半伪功能声明:

Select * from PLACES where distance((LAT, LON), (givenLat, givenLon)) < 100 meters; 
return *[NAME]; 

这是相当简单的,但尽管我使用MySQL的广泛我从来没有使用过的空间扩展。我愿意和他们一起工作,但如果这样做会更快,我也愿意编写自定义函数。上面的查询实际上是我需要制作的。任何超出文档的指针都会非常有帮助,谢谢。

+1

这里http://stackoverflow.com/questions/1006654/fastest-distance-lookup-given-latitude-longitude这里HTTP ://www.movable-type.co.uk/scripts/latlong-db.html,这里http://www.marketingtechblog.com/calculate-distance/和这里http://zcentric.com/2010/03/ 11 /计算距离in-mysql-with-latitude-and-longitude/ – 2012-02-02 00:49:23

+0

谢谢!这些都是很好的资源 - 我想我应该保持一段时间;我还没有全部正确的搜索词汇。我会在系统允许的情况下提供一个总结您的输入的答案。 – SG1 2012-02-02 01:11:27

回答

0

谢尔盖贝纳为这个答案的功劳(我只想关闭这个问题)。他的评论上面链接了几个关于该主题的优秀资源和功能资源。我也发现了这个资源,其完美的作品,如果你在iOS工作:

http://www.thismuchiknow.co.uk/?p=71

0

尽管我不掌握mysql地理空间扩展,但我建议您阅读本演示文稿http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL,因为它包含了我一直非常满意地使用的一个有趣的地理距离函数提议。

SELECT destination.*, 
3956 * 2 * ASIN(SQRT(POWER(SIN((orig.lat - dest.lat) * pi()/180/2), 2) +COS(orig.lat * pi()/180) * COS(dest.lat * pi()/180) * POWER(SIN((orig.lon -dest.lon) * pi()/180/2), 2))) 
asdistanceFROM users destination, users originWHERE origin.id=userid 
and destination.longitudebetween lon1 and lon2 and destination.latitudebetween lat1 and lat2