Golang and php calculate the distance between two latitude and longitude. golang latitude and longitude

Source: Internet
Author: User
Tags acos

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.

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.