In this paper, the method of calculating the distance between two latitude and longitude of Golang and PHP is described. Share to everyone for your reference, as follows:
Golang version:
Copy the Code code as follows:
Package Main
Import (
"FMT"
"Math"
)
Func Main () {
LAT1: = 29.490295
Lng1: = 106.486654
LAT2: = 29.615467
Lng2: = 106.581515
Fmt. Println (Earthdistance (LAT1, Lng1, LAT2, lng2))
}
The unit of return value is in meters
Func earthdistance (LAT1, Lng1, LAT2, lng2 float64) float64 {
Radius: = float64 (6371000)//6378137
rad: = Math. pi/180.0
LAT1 = LAT1 * rad
lng1 = Lng1 * rad
LAT2 = Lat2 * rad
Lng2 = Lng2 * rad
Theta: = Lng2-lng1
Dist: = Math. Acos (Math. Sin (LAT1) * Math. Sin (LAT2) + math. Cos (LAT1) * Math. Cos (LAT2) * Math. Cos (theta))
Return dist * radius
}
PHP Version:
The <?php//return value is in M function pc_sphere_distance ($lat 1, $lon 1, $lat 2, $lon 2, $radius = 6371000) { $rad = Doubleval (m_pi /180.0); $lat 1 = doubleval ($lat 1) * $RAD; $lon 1 = doubleval ($lon 1) * $RAD; $lat 2 = Doubleval ($lat 2) * $RAD; $lon 2 = Doubleval ($lon 2) * $RAD; $theta = $lon 2-$lon 1; $dist = ACOs (sin ($lat 1) * sin ($lat 2) + cos ($lat 1) * cos ($lat 2) * cos ($theta)); return $dist * $radius * 1000;} $lat 1 = 29.490295; $lon 1 = 106.486654; $lat 2 = 29.615467; $lon 2 = 106.581515;echo pc_sphere_distance ($lat 1, $lon 1, $lat 2, $lo N2);
I hope this article will help you with the Go Language program.
The above describes the Golang and PHP to calculate the distance between two latitude and longitude of the method, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.