Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

mysql - Distance units in a query

I have the following query, that selects places withing given radius:

SELECT *
FROM (
  SELECT 
    group, 
    name,  
    111.1111 *
    DEGREES(ACOS(LEAST(1.0, COS(RADIANS(".$lat."))
       * COS(RADIANS(t1.lat))
       * COS(RADIANS(".$lon.") - RADIANS(t1.lon))
       + SIN(RADIANS(".$lat."))
       * SIN(RADIANS(t1.lat))))) AS distance
  FROM t1
) AS grp
WHERE distance < 10

It works well, but I'm not sure if returned results are in miles or kilometers. I suspect KM and if that's the case, how do I convert it to miles?

question from:https://stackoverflow.com/questions/65835476/distance-units-in-a-query

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your code appears to originate in this Stack Overflow answer by O. Jones, which also states:

The constant 111.1111 is the number of kilometres per degree of latitude, based on the old Napoleonic definition of the metre as one ten-thousandth of the distance from the equator to the pole.

Accordingly, the original query uses the name distance_in_km for the result.

Jones goes on to say:

If you want statute miles instead of kilometres, use 69.0 instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...