Distance between two points is calculated in accordance with latitude and longitude coordinates

Now if you have a business need to do is to achieve a calculated distance based positioning coordinates and find a store near you recommended function, known latitude and longitude coordinates stored in the database store. So, this demand has been very clear, in the case of less demanding, as long as the calculated distance between two coordinates, to conduct a sort.

The following will give away one of the core code, is a steak from big brother "google maps" of script code that is used to calculate the distance between two points of latitude and longitude, this distance is not calculated way to get real, theoretical straight-line distance, but the distance has also been very accurate. But, after all, it is the distance obtained by logical calculation, if the required information from the high accuracy of the case, or use third-party maps api interface to obtain more appropriate.

I will turn into golang this code version, and the incoming coordinates restrictions make it more in line with the situation in real life.

package main

import (
	"fmt"
	"log"
	"math"
)

const (
	EARTHRADIUS = 6378.137 //赤道半径
	Degree      = 180.0
)

//以公里为单位
func CalculateTheDistanceByCoordinate(currentLocLat, currentLocLng, intentLocLat, intentLocLng float64) (distance float64, err error) {
	if currentLocLat > 90 || currentLocLat < -90 || intentLocLat > 90 || intentLocLat < -90 {
		err = fmt.Errorf("illegal latitude: currentLocLat %f, intentLocLat %f\n", currentLocLat, intentLocLat)
		return
	}
	if currentLocLng > 180 || currentLocLng < -180 || intentLocLng > 180 || intentLocLng < -180 {
		err = fmt.Errorf("illegal longitude: currentLocLng %f, intentLocLng %f\n", currentLocLng, intentLocLng)
		return
	}

	var rad = func(coordinate float64) (radian float64) {
		return coordinate * math.Pi / Degree
	}

	differenceLocLat := rad(currentLocLat) - rad(intentLocLat)
	differenceLocLng := rad(currentLocLng) - rad(intentLocLng)

	distance = 2 * math.Asin(math.Sqrt(math.Pow(math.Sin(differenceLocLat/2), 2) +
		math.Cos(rad(currentLocLat))*math.Cos(rad(intentLocLat))*math.Pow(math.Sin(differenceLocLng/2), 2)))

	distance = math.Round(distance*EARTHRADIUS*10000) / 10000

	return
}
package main

import (
	"github.com/smartystreets/goconvey/convey"
	"testing"
)

func TestCalculateTheDistanceByCoordinate(t *testing.T) {
	convey.Convey("CalculateTheDistanceByCoordinate", t, func(c convey.C) {
		c.Convey("normal", func(c convey.C) {
			m, _ := CalculateTheDistanceBetweenSellerStoresByCoordinate(22.982236862182617, 113.34020233154297,
				22.982236862182617, 113.34020233154297)
			c.So(m, convey.ShouldBeZeroValue)

			m, _ = CalculateTheDistanceBetweenSellerStoresByCoordinate(22.982236862182617, 113.34020233154297,
				-15.47, -47.56)
			c.So(m, convey.ShouldEqual, 17865.4643)
		})

		c.Convey("abnormal", func(c convey.C) {
			m, err := CalculateTheDistanceBetweenSellerStoresByCoordinate(22.982236862182617, 113.34020233154297,
				400, 400)
			c.So(m, convey.ShouldNotBeEmpty)
			c.So(err, convey.ShouldNotBeNil)

			m, err = CalculateTheDistanceBetweenSellerStoresByCoordinate(22.982236862182617, 113.34020233154297,
				-10, 400)
			c.So(m, convey.ShouldNotBeEmpty)
			c.So(err, convey.ShouldNotBeNil)
		})
	})
}



reference

https://www.cnblogs.com/zhoug2020/p/7634187.html
https://blog.csdn.net/zhuxiaoping54532/article/details/53671641

Published 22 original articles · won praise 6 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_42403866/article/details/102702191