PHP calculates the distance between its own longitude x and y based on the information x and y in the database, and then outputs data less than 5000 meters.

Source: Internet
Author: User
PHP calculates the distance between its own longitude x and y based on the information x and y in the database, output data less than 5000 meters. PHP calculates the distance between its own longitude x and y and the information x and y in the database, and then outputs data less than 5000 meters.

Currently, JS is used to obtain your own location.
Window. onload = function (){
Var options = {
EnableHighAccuracy: true,
MaximunAge: 1000,
Timeout: 5000
};
If (window. navigator. geolocation ){
Navigator. geolocation. getCurrentPosition (successCallback, errorCallback, options );
} Else {
Alert ("Your browser does not support location! ");
}
}
Function successCallback (position ){
Var output = "";
Output + = "Your position has bean located. \ r \ n ";
Output + = "Latitude:" + position. coords. latitude + "";
Output + = "longpolling:" + position. coords. longpolling + "";
Output + = "Accuracy:" + position. coords. accuracy + "meters ";
If (position. coords. latitude ){
Output + = "Accuracy:" + position. coords. altitudeAccuracy + "meters ";
}
If (position. coords. heading ){
Output + = "Heading:" + position. coords. Heading + "meters ";
}
If (position. coords. speed ){
Output + = "Speed:" + position. coords. Speed + "m/s ";
}
Output + = "Time of Position" + position. timestamp + "m/s ";
Alert (output );
}

The table_map table records many locations. The fields are Latitude and longpolling.

Now I need to output information less than 5000 meters with myself. give me some ideas !!


Reply to discussion (solution)

The following formula is used to calculate 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 unit of R and Distance is the same. if 6371.004 km is used as the radius, Distance is the unit of km.


The following formula is used to calculate 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 unit of R and Distance is the same. if 6371.004 km is used as the radius, Distance is the unit of km.



Can't read... Is this PHP?

This is a mathematical formula
Ask your math teacher if you don't understand it.


The following formula is used to calculate 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 unit of R and Distance is the same. if 6371.004 km is used as the radius, Distance is the unit of km.



Can't read... Is this PHP?

Do you understand the sine and cosine?

Similar to a merchant APP, the function is to calculate the user distance from a specific merchant location. I use js for implementation, and no backend is used.

/Convert the longitude and latitude to the distance calculation function Rad (d) {return d * Math. PI/180.0; // Convert the longitude and latitude to the moderately partitioned form of trigonometric function. } // Calculate the distance. the parameters are the latitude and longitude of the first vertex, and the latitude of the second vertex. The longitude function GetDistance (lat1, lng1, lat2, lng2) {var radLat1 = Rad (lat1); var radLat2 = Rad (lat2); var a = radLat1-radLat2; var B = Rad (lng1)-Rad (lng2 ); var s = 2 * Math. asin (Math. sqrt (Math. pow (Math. sin (a/2), 2) + Math. cos (radLat1) * Math. cos (radLat2) * Math. pow (Math. sin (B/2), 2); s = s * 6378.137; // EARTH_RADIUS; s = Math. round (s * 10000)/10000; // The output is km // s = s. toFixed (4); return s ;}

This is a related function.


My current method is:
The stored procedure for calculating the distance between two points in the database (executed before adding the stored procedure)
Set global log_bin_trust_function_creators = 1;
)
Then, call this function directly in the query.

/*
* Calculate the poi point distance (unit: km)
*
*/
DELIMITER $
Create definer = CURRENT_USER FUNCTION 'getdistance '('lon1' float, 'lat1' float, 'lon2 'float, 'lat2' float)
RETURNS double
Begin
Declare d double;
Declare radius int;
If lon1 = 0 or lat1 = 0 or lon2 = 0 or lat2 = 0 then
Return 99999999;
End if;
Set radius = 6378140; # assume that the Earth is a positive spherical shape with a diameter of 6378140 meters
Set d = (2 * ATAN2 (SQRT (SIN (lat1-lat2) * PI ()/180/2)
* SIN (lat1-lat2) * PI ()/180/2) +
COS (lat2 * PI ()/180) * COS (lat1 * PI ()/180)
* SIN (lon1-lon2) * PI ()/180/2)
* SIN (lon1-lon2) * PI ()/180/2 )),
SQRT (1-SIN (lat1-lat2) * PI ()/180/2)
* SIN (lat1-lat2) * PI ()/180/2)
+ COS (lat2 * PI ()/180) * COS (lat1 * PI ()/180)
* SIN (lon1-lon2) * PI ()/180/2)
* SIN (lon1-lon2) * PI ()/180/2) * radius;
Return round (d/1000, 2 );
END $
DELIMITER $;

Of course, the premise is that each user has a latitude and longitude attribute.

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.