PHP calculates the Distance Based on the longitude and latitude between two points, and php calculates the longitude and latitude of two points.

Source: Internet
Author: User

PHP calculates the Distance Based on the longitude and latitude between two points, and php calculates the longitude and latitude of two points.

This is a good example. paste the code to know the latitude value and longitude value first.

/*** @ Desc calculate the distance * @ param float $ lat latitude value * @ param float $ lng longitude value */function getDistance ($ lat1, $ lng1, $ lat2, $ lng2) {$ earthRadius = 6367000; // approximate radius of earth in meters/* Convert these degrees to radians to work with the formula */$ lat1 = ($ lat1 * pi ()/180; $ lng1 = ($ lng1 * pi ()/180; $ lat2 = ($ lat2 * pi ()/180; $ lng2 = ($ lng2 * pi ()) /180;/* Using the Haversine formula http://en.wikipedia.org/wiki/Haversine_formula calculate the distance */$ calclong133 = $ lng2-$ lng1; $ calcLatitude = $ lat2-$ lat1; $ stepOne = pow (sin ($ calcLatitude/2), 2) + cos ($ lat1) * cos ($ lat2) * pow (sin ($ calcLongitude/2 ), 2); $ stepTwo = 2 * asin (min (1, sqrt ($ stepOne); $ calculatedDistance = $ earthRadius * $ stepTwo; return round ($ calculatedDistance );}

How to Use php to calculate the distance between two longitude and latitude

This is not counted, but the code finds some problems in your code.
First, the linear distance between the two points is square: $ len is useless after calculation, and there is no return, it is useless.
2. If you enter the same longitude and latitude, the result is not 0 and it is still a large number, so there should be a problem.

You can refer to Baidu's "php calculates the distance between two points by longitude and latitude", there are a lot of ready-made code
I think this introduction is more detailed, and there is code, you can look at it
Blog.csdn.net/..963740

If we know the longitude and latitude of two points, how can we calculate the distance between two points?

Longitude at the same latitude (except for the Equator)
H X 111 x cosd = G (h = longitude difference between two locations D = local geographic latitude G = actual distance)

Cross-latitude triangle
For example, AB has two different longitude and latitude (A longitude and B latitude)
First, we can calculate the distance between the latitude B 'that is collocated with the point. After calculating the distance between A and B, we can obtain the result by using the stock theorem.

In short, the following general formulas are available:
Formula for distance between two points on the earth:
The longitude of the earth is A1 and A2 (positive E and negative W), and the latitude is B1 and B2 (positive N and negative S ).
Make A0 = (A1-A2) limit 2, B0 = (BI-B2) limit 2
F = √ sinB0 × sinB0 + cosB1 × cosB2 × sinA0 × sinA0

Then
1. linear distance between two points = 2fR
2. Minimum spherical distance between two points = arcsinf degrees 90 ° × degrees R (angle)
3. Minimum spherical distance between two points = arcsinf × 2R (radian)

Note: E, W, N, S = east-west north, R = Earth's radius; √ = root number; region = circumference rate.
Enter the formula and calculate it by 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.