Golang and php calculate the distance between two latitude and longitude. golang latitude and longitude
This article describes how golang and php calculate the distance between two latitudes. We will share this with you for your reference. The details are as follows:
Golang version:
Copy codeThe Code is 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 the returned value is meter.
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:
<? Php // The unit of the returned value is meter function pc_sphere_distance ($ lat1, $ lon1, $ lat2, $ lon2, $ radius = 6371000) {$ rad = doubleval (M_PI/180.0 ); $ lat1 = doubleval ($ lat1) * $ rad; $ lon1 = doubleval ($ lon1) * $ rad; $ lat2 = doubleval ($ lat2) * $ rad; $ lon2 = doubleval ($ lon2) * $ rad; $ theta = $ lon2-$ lon1; $ dist = acos (sin ($ lat1) * sin ($ lat2) + cos ($ lat1) * cos ($ lat2) * cos ($ theta); return $ dist * $ radius * 1000;} $ lat1 = 29.490295; $ lon1 = 106.486654; $ lat2 = 29.615467; $ lon2 = 106.581515; echo pc_sphere_distance ($ lat1, $ lon1, $ lat2, $ lon2 );
I hope this article will help you with the Go language programming.