1 Star 0 Fork 0

szmaozi/go-utils

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
gps.go 5.29 KB
Copy Edit Raw Blame History
szmaozi authored 2021-09-02 13:41 . wip: merge biob-v1
/*
* @Name:
* @Descripttion:
* @Warning:
* @version:
* @Author: moo
* @Date: 2021-01-11 13:56:53
* @LastEditors: moo
* @LastEditTime: 2021-01-11 15:50:00
*/
package utils
import (
"math"
)
const (
pi float64 = 3.1415926535897932384626
x_pi float64 = 3.14159265358979324 * 3000.0 / 180.0
a float64 = 6378245.0
ee float64 = 0.00669342162296594323
)
type GPSMODE int
const (
GPSMODE_WGS84 GPSMODE = 0 // 国际标准,GPS坐标(Google Earth使用、或者GPS模块)
GPSMODE_GCJ02 GPSMODE = 1 // 中国坐标偏移标准,Google Map、高德、腾讯使用
GPSMODE_BD09 GPSMODE = 2 // 百度坐标偏移标准
)
/**
* GPS84、高德、百度、腾讯编码互转
*/
func transformLat(x float64, y float64) float64 {
var ret float64
ret = -100.0 + 2.0*x + 3.0*y + 0.2*y*y + 0.1*x*y + 0.2*math.Sqrt(math.Abs(x))
ret += (20.0*math.Sin(6.0*x*pi) + 20.0*math.Sin(2.0*x*pi)) * 2.0 / 3.0
ret += (20.0*math.Sin(y*pi) + 40.0*math.Sin(y/3.0*pi)) * 2.0 / 3.0
ret += (160.0*math.Sin(y/12.0*pi) + 320*math.Sin(y*pi/30.0)) * 2.0 / 3.0
return ret
}
func transformLon(x float64, y float64) float64 {
var ret float64
ret = 300.0 + x + 2.0*y + 0.1*x*x + 0.1*x*y + 0.1*math.Sqrt(math.Abs(x))
ret += (20.0*math.Sin(6.0*x*pi) + 20.0*math.Sin(2.0*x*pi)) * 2.0 / 3.0
ret += (20.0*math.Sin(x*pi) + 40.0*math.Sin(x/3.0*pi)) * 2.0 / 3.0
ret += (150.0*math.Sin(x/12.0*pi) + 300.0*math.Sin(x/30.0*pi)) * 2.0 / 3.0
return ret
}
func transformGps(lat float64, lon float64) []float64 {
if outOfChina(lat, lon) {
return []float64{lat, lon}
}
dLat := transformLat(lon-105.0, lat-35.0)
dLon := transformLon(lon-105.0, lat-35.0)
radLat := lat / 180.0 * pi
magic := math.Sin(radLat)
magic = 1 - ee*magic*magic
sqrtMagic := math.Sqrt(magic)
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi)
dLon = (dLon * 180.0) / (a / sqrtMagic * math.Cos(radLat) * pi)
mgLat := lat + dLat
mgLon := lon + dLon
return []float64{mgLat, mgLon}
}
/**
* 判断是否在中国
* @param lat
* @param lon
* @return
*/
func outOfChina(lat float64, lon float64) bool {
if lon < 72.004 || lon > 137.8347 {
return true
}
if lat < 0.8293 || lat > 55.8271 {
return true
}
return false
}
/**
* 84 ==》 高德/腾讯
* @param lat
* @param lon
* @return
*/
func Gps84ToGcj02(lat float64, lon float64) []float64 {
if outOfChina(lat, lon) {
return []float64{lat, lon}
}
dLat := transformLat(lon-105.0, lat-35.0)
dLon := transformLon(lon-105.0, lat-35.0)
radLat := lat / 180.0 * pi
magic := math.Sin(radLat)
magic = 1 - ee*magic*magic
sqrtMagic := math.Sqrt(magic)
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi)
dLon = (dLon * 180.0) / (a / sqrtMagic * math.Cos(radLat) * pi)
mgLat := lat + dLat
mgLon := lon + dLon
return []float64{mgLat, mgLon}
}
/**
* 高德/腾讯 ==》 84
* @param lon * @param lat * @return
* */
func Gcj02ToGps84(lat float64, lon float64) []float64 {
gps := transformGps(lat, lon)
lontitude := lon*2 - gps[1]
latitude := lat*2 - gps[0]
return []float64{latitude, lontitude}
}
/**
* 高德/腾讯 == 》 百度
* @param lat
* @param lon
*/
func Gcj02ToBd09(lat float64, lon float64) []float64 {
x := lon
y := lat
z := math.Sqrt(x*x+y*y) + 0.00002*math.Sin(y*x_pi)
theta := math.Atan2(y, x) + 0.000003*math.Cos(x*x_pi)
tempLon := z*math.Cos(theta) + 0.0065
tempLat := z*math.Sin(theta) + 0.006
gps := []float64{tempLat, tempLon}
return gps
}
/**
* 百度 == 》 高德/腾讯
* @param lat
* @param lon
*/
func Bd09ToGcj02(lat float64, lon float64) []float64 {
x := lon - 0.0065
y := lat - 0.006
z := math.Sqrt(x*x+y*y) - 0.00002*math.Sin(y*x_pi)
theta := math.Atan2(y, x) - 0.000003*math.Cos(x*x_pi)
tempLon := z * math.Cos(theta)
tempLat := z * math.Sin(theta)
gps := []float64{tempLat, tempLon}
return gps
}
/**
* 84 == 》 百度
* @param lat
* @param lon
* @return
*/
func Gps84ToBd09(lat float64, lon float64) []float64 {
gcj02 := Gps84ToGcj02(lat, lon)
bd09 := Gcj02ToBd09(gcj02[0], gcj02[1])
return bd09
}
/**
* 百度 == 》 84
* @param lat
* @param lon
* @return
*/
func Bd09ToGps84(lat float64, lon float64) []float64 {
gcj02 := Bd09ToGcj02(lat, lon)
gps84 := Gcj02ToGps84(gcj02[0], gcj02[1])
//保留小数点后六位
gps84[0] = retain6(gps84[0])
gps84[1] = retain6(gps84[1])
return gps84
}
/*
* 保留小数点后六位
* @param num
* @return
*/
func retain6(num float64) float64 {
// result := fmt.Sprintf("%.6f", num)
// return Double.valueOf(result)
result := TruncFloat(num, 0.000001)
return result
}
/**
func main() {
a1 := gps84_To_Gcj02(31.3055205, 121.1405001)
fmt.Println(a1)
a1 = gps84_To_Gcj02(31.3051180, 121.1404452)
fmt.Println(a1)
a1 = gps84_To_Gcj02(31.3053301, 121.1408431)
fmt.Println(a1)
a1 = gps84_To_Gcj02(31.3053266, 121.1392659)
fmt.Println(a1)
a1 = gps84_To_Gcj02(31.3039517, 121.1389722)
fmt.Println(a1)
a1 = gps84_To_Gcj02(31.2996405, 121.1384954)
fmt.Println(a1)
a1 = gps84_To_Gcj02(31.3065345, 121.1413582)
fmt.Println(a1)
}
**/
func EarthDistance(lat1, lng1, lat2, lng2 float64) float64 {
// radius := 6378.137 //return: km
radius := float64(6378137) //return: m
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/szmaozi/go-utils.git
git@gitee.com:szmaozi/go-utils.git
szmaozi
go-utils
go-utils
29e02a007caf

Search