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

使用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
    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.

    点赞 评论
  • drphfy1198
    drphfy1198 2019-06-19 16:46
    CREATE FUNCTION fnDistanceMiles(lat1 DECIMAL(12, 7),
                                    lon1 DECIMAL(12, 7),
                                    lat2 DECIMAL(12, 7),
                                    lon2 DECIMAL(12, 7))
    RETURNS DECIMAL(16, 8)
    BEGIN
    
    DECLARE DISTANCE DECIMAL(11, 4) DEFAULT 0;
    
    SELECT 69.04117454 *
           DEGREES(ACOS(LEAST(COS(RADIANS(lat1))
                              * COS(RADIANS(lat2))
                              * COS(RADIANS(lon1 - lon2))
                              + SIN(RADIANS(lat1))
                              * SIN(RADIANS(lat2)), 1.0)))
    INTO DISTANCE;
    
    RETURN DISTANCE;
    
    END;
    
    DELIMITER ;
    
    点赞 评论

相关推荐