The go language calculates the distance between two longitude and latitude _golang

Source: Internet
Author: User
Tags cos sin

The example in this article describes how the go language calculates the distance between two longitude and latitude. Share to everyone for your reference. The implementation methods are as follows:

Copy 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))
}

Func earthdistance (LAT1, Lng1, LAT2, lng2 float64) float64 {
Radius: = 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
}

I hope this article will help you with your go language program.

Related Article

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.