duai3681 2015-03-04 11:52
浏览 250
已采纳

使用st_distance在mysql中按距离对用户进行排序

I've done a bit of reading on this but there's hardly any information on doing it using points and the st_distance function in mysql. I suppose this makes sense since according to http://bugs.mysql.com/bug.php?id=70494 it wasn't even documented until halfway through last year. Almost every solution I've seen instead uses separate long and lat columns, and then puts them into the haversine formula like this: sorting distance in MySQL PHP This seems like a really messy way of doing it though due to the existance of geometry data types and the spatial functions.

My table so far is:

Username - varchar

Location - Point(1 1) or something like that

I want to choose one user and then display the nearest 10 or so users to them. Is it possible to do this entirely through SQL using points and the st_distance function, or will I have to make some changes and use the haversine formula instead?

  • 写回答

2条回答 默认 最新

  • dongxian7471 2015-03-04 12:19
    关注

    Is it possible to do this entirely through SQL using points and the st_distance function, or will I have to make some changes and use the haversine formula instead?

    As documented under Geometry Class:

    Geometry is the root class of the hierarchy. It is a noninstantiable class but has a number of properties, described in the following list, that are common to all geometry values created from any of the Geometry subclasses.

    [ deletia ]

    All calculations are done assuming Euclidean (planar) geometry.

    [ deletia ]

    For example, in different coordinate systems, the distance between two objects may differ even when objects have the same coordinates, because the distance on the planar coordinate system and the distance on the geocentric system (coordinates on the Earth's surface) are different things.

    Therefore MySQL's spatial extensions (including its ST_Distance() function) cannot be used to calculate geodesics such as great-circle distance, which is what you are after. You will, as you infer, have to use a formula such as Haversine instead.

    There is a good tutorial on the Google Developers website: Creating a Store Locator with PHP, MySQL & Google Maps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大