Home >Backend Development >PHP Tutorial >PHP calculates the distance based on the longitude and latitude between two points, PHP calculates the longitude and latitude of two points_PHP tutorial

PHP calculates the distance based on the longitude and latitude between two points, PHP calculates the longitude and latitude of two points_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:15:331145browse

PHP calculates the distance based on the longitude and latitude between two points, PHP calculates the longitude and latitude of two points

This is a good example. Paste the code directly. First you need to know the latitude and longitude values

/** 
* @desc 根据两点间的经纬度计算距离 
* @param float $lat 纬度值 
* @param float $lng 经度值 
*/ 
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 
*/ 

$calcLongitude = $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); 
} 

php calculates the distance between two longitudes and latitudes, guide

I don’t know how to calculate this thing, but I found some problems in your code when I looked at the code.
First, the square of the straight-line distance between two points: $len is not used after the calculation, and there is no return. It is completely useless.
Second, enter the same longitude and latitude, and the result is not 0, and it is still a very large number, so there should be something wrong

You can use Baidu to calculate the distance between two points by "php" ”, there are a lot of ready-made codes
I think this introduction is more detailed, and there are codes, you can take a look
blog.csdn.net/...963740

How to calculate the distance between two points if you know their longitude and latitude

Same latitude and different longitudes (except the equator)
h X 111 A triangle
For example, two points AB have different longitudes and latitudes (A longitude and B latitude)
Then first calculate the distance of the latitude B' that is collinear with point A, and then calculate the distance from A to B', then use The Pythagorean Theorem can be used to derive

Simply speaking, the following general formula can be used:
The distance formula between any two points on the earth:
The longitudes of any two points on the earth are A1 and A2 (E is positive , W negative), the latitudes are B1 and B2 (N positive, S negative) respectively.
Let A0=(A1-A2)÷2, B0=(BI-B2)÷2
f=√sinB0×sinB0+cosB1×cosB2×sinA0×sinA0

then
1. Straight-line distance in space between two points = 2fR
2. Minimum spherical distance between two points = arcsinf÷90°×∏R (angle)
3. Minimum spherical distance between two points = arcsinf×2R (radians)

Explanation: E, W, N, S = east, west, north, south; R = radius of the earth; √ = square root; ∏ = pi.
Put in the formula and do the calculation yourself


http://www.bkjia.com/PHPjc/904006.html

truehttp: //www.bkjia.com/PHPjc/904006.htmlTechArticlePHP calculates the distance based on the longitude and latitude between two points. PHP calculates the longitude and latitude of two points. This is a good example. Post it directly. Code, first you need to know the latitude value and longitude value/** * @desc According to the distance between two points...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn