MySQL calculates the distance between latitude and longitude highlights

Source: Internet
Author: User
Tags asin cos pow sin

1, calculate the distance formula is long (on-line search), build a MySQL function:

Delimiter $ $CREATE FUNCTION fun_jw_dist (lng1 double (15,9), LAT1 double (9), Lng2 double (15,9), Lat2 double (15,9))  RETURNS double (15,9) BEGIN DECLARE Dist Double (9);  SET dist = round (6378.138*2*asin (sqrt (Pow ((Lat1*pi ()/180-lat2*pi ()/180)/2), 2) +cos (Lat1*pi ()/180) *cos (Lat2*pi ( )/180) * POW (Sin ((Lng1*pi ()/180-lng2*pi ()/180)/2), 2)) (*1000));  RETURN (Dist); end$ $delimiter;
Test:

For example:

1th (116.359751000, 39.936868000) 2nd (117.291179000, 35.657141000)
Executes a select round (6378.138*2*asin (sqrt (39.936868000*pi ()/180-35.657141000*pi ()/180)/2 in the database, 2) +cos ( 39.936868000*pi ()/180) *cos (35.657141000*pi ()/180) * POW (Sin ((116.359751000*pi ()/180-117.291179000*pi ()/180)/2), 2 ))) *1000)

Execution Result: 483402

Performed: Select Fun_jw_dist (116.359751000, 39.936868000, 117.291179000, 35.657141000);

Execution Result: 483402.000000000


MySQL calculates the distance between latitude and longitude highlights

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.