Mysql functions-distance calculation based on latitude and longitude coordinates

Source: Internet
Author: User
Tags mysql functions
Mysql functions-distance calculation based on latitude and longitude coordinates in actual Web development, more and more professional algorithms are coming into use in some specific fields. For example, the distance calculation method based on two points of latitude and longitude is mentioned in this article. Two common computations: 1. The distance to be calculated is the distance of the elliptical sphere. the longitude and latitude of Point A is (LonA, LatA), and the longitude and latitude of point B is (LonB, LatB

Mysql functions-distance calculation based on latitude and longitude coordinates in actual Web development, more and more professional algorithms are coming into use in some specific fields. For example, the distance calculation method based on two points of latitude and longitude is mentioned in this article. Two common computations: 1. The distance to be calculated is the distance of the elliptical sphere. the longitude and latitude of Point A is (LonA, LatA), and the longitude and latitude of point B is (LonB, LatB

Mysql functions-distance calculation based on latitude and longitude coordinates
In actual Web development, there are more and more professional algorithms in some specific fields. For example, the distance calculation method mentioned in this article is based on two dimensions.
Two common computations:
1. The distance to be calculated is the elliptical distance.
Set the longitude and latitude of the first Vertex A to (LonA, LatA), and the longitude and latitude of the second vertex B to (LonB, LatB) based on the zero-degree longitude line, the Longitude is a positive value (longpolling), the Longitude is a negative value (-longpolling), and the Latitude is 90-Latitude ), when the Latitude value is 90 + (90 + Latitude), the two points after processing are counted as (MLonA, MLatA) and (MLonB, MLatB ). According to the triangle derivation, the following formula is obtained for calculating the distance between two points:
C = sin (MLatA) * sin (MLatB) * cos (MLonA-MLonB) + cos (MLatA) * cos (MLatB)
Distance = R * Arccos (C) * Pi/180
Here, the units of R and Distance are the same. If 6371.004 km is used as the radius, Distance is the unit of kilometer. If other units such as mile are used, unit conversion is also required, 1Km = 0.621371192 mile
If only the longitude is processed plus and minus, And the Latitude is not processed with 90-Latitude (assuming they are all in the northern hemisphere, and the southern hemisphere is only available in Australia), the formula will be:
C = sin (LatA) * sin (LatB) + cos (LatA) * cos (LatB) * cos (MLonA-MLonB)
Distance = R * Arccos (C) * Pi/180

2. Methods provided by google map (PHP version)
class Distance{const EARTH_RADIUS = 6378.137; // earth radius (const) kilometer/** * *Example: *$precision = 49; *ini_set("precision", $precision); *echo pi(); //will output 3.141592653589793115997963468544185161590576171875 * *1 kilometer = 0.621371192 mile * *@param double d *@return double data */private function rad( $d ){return $d * M_PI / 180.0;}/** * *Example : get_distance(44.2112,-88.4175,34.5082,-82.6498) *Get Distance between two point : longitude,latitude(lat1,lng1 => lat2,lng2 ) * *@param double d *@return double data * */public function get_distance( $lat1, $lng1, $lat2, $lng2, $base = 1000 ){$radLat1 = floatval( $this->rad($lat1) );$radLat2 = floatval( $this->rad($lat2) );$a = $radLat1 - $radLat2;$b = $this->rad($lng1) - $this->rad($lng2);$s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));$s = $s * self::EARTH_RADIUS;$s = round($s * 10000) / 10000;return $s;}}


The first method is cumbersome to calculate. We usually use the second method. However, either the first or the second type can be converted directly using a programming language, which is time-consuming because we usually have to identify all the results, then calculate and filter the results one by one to obtain the result set. If you need to sort the results, it will take more time.
An optimization method is to use SQL functions. For specific Mysql functions, see the appendix. Note that the brackets in the appendix are measured in kilometers by default. If you want to convert them into miles, please refer to the PHP Code comments section in this article to modify it yourself.

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.