各地图平台间的坐标转换
腾讯和高德坐标是一样的
# 百度坐标转高德坐标
// 百度坐标转高德(传入经度、纬度)
export function baidu_to_gaode(bd_lng, bd_lat) {
const X_PI = (Math.PI * 3000.0) / 180.0;
const x = bd_lng - 0.0065;
const y = bd_lat - 0.006;
const z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * X_PI);
const theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * X_PI);
const gg_lng = z * Math.cos(theta);
const gg_lat = z * Math.sin(theta);
return { lng: gg_lng, lat: gg_lat };
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 百度坐标转腾讯坐标
// 百度坐标系转腾讯坐标
baidu_to_tengxun(lng, lat) {
let x_pi = (3.14159265358979324 * 3000.0) / 180.0;
let x = lng - 0.0065;
let y = lat - 0.006;
let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
let theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
let lngs = z * Math.cos(theta);
let lats = z * Math.sin(theta);
return [lngs, lats]
}
//使用方法
baidu_to_tengxun(经度,纬度)
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 高德坐标转百度坐标
// 高德坐标转百度(传入经度、纬度)
export function gaode_to_baidu(gg_lng, gg_lat) {
const X_PI = (Math.PI * 3000.0) / 180.0;
const x = gg_lng;
const y = gg_lat;
const z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * X_PI);
const theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * X_PI);
const bd_lng = z * Math.cos(theta) + 0.0065;
const bd_lat = z * Math.sin(theta) + 0.006;
return {
lat: bd_lat,
lng: bd_lng,
};
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 坐标转换WGS84 → GCJ02
浏览器坐标WGS84 → GCJ02(微信/高德)
/**
* 坐标转换
*/
class CoordTransform
{
const PI = 3.1415926535897932384626;
const X_PI = 19930328.0 / 6000000.0;
const A = 6378245.0; // 克拉索夫斯基椭球长半轴
const EE = 0.00669342162296594323; // 第一偏心率平方
/**
* WGS84 → GCJ02
*
* @param float $lng WGS84 经度
* @param float $lat WGS84 纬度
* @return array [gcj_lng, gcj_lat]
*/
public static function wgs84ToGcj02(float $lng, float $lat): array
{
if (self::outOfChina($lng, $lat)) {
return [$lng, $lat];
}
$dLat = self::transformLat($lng - 105.0, $lat - 35.0);
$dLng = self::transformLng($lng - 105.0, $lat - 35.0);
$radLat = $lat / 180.0 * self::PI;
$magic = sin($radLat);
$magic = 1 - self::EE * $magic * $magic;
$sqrtMagic = sqrt($magic);
$dLat = ($dLat * 180.0) / ((self::A * (1 - self::EE)) / ($magic * $sqrtMagic) * self::PI);
$dLng = ($dLng * 180.0) / (self::A / $sqrtMagic * cos($radLat) * self::PI);
return [
$lng + $dLng,
$lat + $dLat
];
}
/**
* 判断是否在中国境内
*/
private static function outOfChina(float $lng, float $lat): bool
{
return !(
$lng > 73.66 &&
$lng < 135.05 &&
$lat > 3.86 &&
$lat < 53.55
);
}
/**
* 纬度偏移量
*/
private static function transformLat(float $x, float $y): float
{
$ret = -100.0 + 2.0 * $x + 3.0 * $y
+ 0.2 * $y * $y
+ 0.1 * $x * $y
+ 0.2 * sqrt(abs($x));
$ret += (20.0 * sin(6.0 * $x * self::PI)
+ 20.0 * sin(2.0 * $x * self::PI)) * 2.0 / 3.0;
$ret += (20.0 * sin($y * self::PI)
+ 40.0 * sin($y / 3.0 * self::PI)) * 2.0 / 3.0;
$ret += (160.0 * sin($y / 12.0 * self::PI)
+ 320.0 * sin($y * self::PI / 30.0)) * 2.0 / 3.0;
return $ret;
}
/**
* 经度偏移量
*/
private static function transformLng(float $x, float $y): float
{
$ret = 300.0 + $x + 2.0 * $y
+ 0.1 * $x * $x
+ 0.1 * $x * $y
+ 0.1 * sqrt(abs($x));
$ret += (20.0 * sin(6.0 * $x * self::PI)
+ 20.0 * sin(2.0 * $x * self::PI)) * 2.0 / 3.0;
$ret += (20.0 * sin($x * self::PI)
+ 40.0 * sin($x / 3.0 * self::PI)) * 2.0 / 3.0;
$ret += (150.0 * sin($x / 12.0 * self::PI)
+ 300.0 * sin($x / 30.0 * self::PI)) * 2.0 / 3.0;
return $ret;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
上次更新: 2026/06/01, 17:20:54