# How to calculate the distance between two latitude and longitude using PHP

Source: Internet
Author: User
Tags cos sin
Calculates the distance between two latitude and longitude by using PHP.

/*

* Calculate the distance between two latitude and longitude (in meters)

* */

`<strong>public function </strong>getdistanceaction () {    \$lng 1=117.27;  Longitude 1    \$lat 1=31.86;   Latitude 1    \$lng 2=120.19;  Longitude 2    \$lat 2=30.26;   Latitude 2    \$EARTH _radius = 6378137;   Earth radius    \$RAD = Pi ()/180.0;    \$radLat 1 = \$lat 1 * \$RAD;    \$radLat 2 = \$lat 2 * \$RAD;    \$a = \$radLat 1-\$radLat 2;    Two points latitude difference    \$b = (\$lng 1-\$lng 2) * \$RAD;  Two-point longitude difference    \$s = 2 * ASIN (sqrt (Pow (sin (\$a/2), 2) + cos (\$radLat 1) * cos (\$radLat 2) * POW (sin (\$b/2), 2)));    \$s = \$s * \$EARTH _radius;    \$s = round (\$s * 10000)/10000;    Print_r (\$s);   Correct answer: 330518.674}`

This article describes the use of PHP to calculate the distance between two latitude and longitude, more relevant content please follow the PHP Chinese web.

Related Keywords:
Related Article

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.