Golang and PHP to calculate the distance between two latitude and longitude method

Source: Internet
Author: User
Tags acos cos
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.

  • 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.