[原]ios原生定位转换为高德地图坐标

今天在做项目的时候遇到一个问题
客户反馈:他在使用签到(基于高德地图上下班打卡的移动HTML5应用)的过程中定位不准的问题.通过网络搜索发现IOS使用的是地心坐标,而高德地图的是火星坐标,所以直接使用ios手机GPS定位的坐标在高德上就会出现误差,Android定位不需要进行转换,因为她自己就是使用的火星坐标,更多详细信息请

查看

经过测试误差在:300+米

这样肯定是不行的

下面直接给出解决方法:http://blog.csdn.net/xiaobaismiley/article/details/37576303
通过对此博客代码的研究,将其C++程序修改为JavaScript程序。
另外高德地图提供了一个API可以进行直接转换:但是会发送一次网络请求。

如下代码是es6编写,如果要兼容所有浏览器需要进行babel转码,下来代码中讲2种方法都进行了封装

JavaScript

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
/**
* 高德地图封装
*
* @class Maps
*/
class Maps {
constructor(option) {
this.option = option;
this.gdmap = new AMap.Map(this.option.dom, this.option.setting);//高德地图init
this.pi = 3.1415926535897932384626;//圆周率
this.ee = 0.00669342162296594323;//
this.a = 6378245.0;// 长半轴,地球是个椭圆
}
/**
* IOS坐标地图转换为高德地图坐标,高德地图自己的方法
*
* @returns
*
* @memberOf Maps
*/
convertAdress(option) {
return new Promise(function (resolve, reject) {
AMap.convertFrom(option.posi, "gps", function (status, result) {
resolve(result);
});
})
}

/**
* 地球坐标转换为火星坐标
* World Geodetic System ==> Mars Geodetic System
*
* @param wgLat 地球坐标
* @param wgLon
*
* mglat,mglon 火星坐标
*/
earthTransformMars(wgLat, wgLon) {
let mgLat ='',
mgLon = '';
if (this.outOfChina(wgLat, wgLon)) {
mgLat = wgLat;
mgLon = wgLon;
return;
}
let dLat = this.transformLat(wgLon - 105.0, wgLat - 35.0),
dLon = this.transformLon(wgLon - 105.0, wgLat - 35.0),
radLat = wgLat / 180.0 * this.pi,
magic = Math.sin(radLat);
magic = 1 - this.ee * magic * magic;
let sqrtMagic = Math.sqrt(magic);
dLat = (dLat * 180.0) / ((this.a * (1 - this.ee)) / (magic * sqrtMagic) * this.pi);
dLon = (dLon * 180.0) / (this.a / sqrtMagic * Math.cos(radLat) * this.pi);
mgLat = wgLat + dLat;
mgLon = wgLon + dLon;
return [mgLat,mgLon]
}
outOfChina(lat, lon) {
if (lon < 72.004 || lon > 137.8347)
return true;
if (lat < 0.8293 || lat > 55.8271)
return true;
return false;
}
transformLat(x, y) {
let 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 * this.pi) + 20.0 * Math.sin(2.0 * x * this.pi)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(y * this.pi) + 40.0 * Math.sin(y / 3.0 * this.pi)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(y / 12.0 * this.pi) + 320 * Math.sin(y * this.pi / 30.0)) * 2.0 / 3.0;
return ret;
}

transformLon(x, y) {
let 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 * this.pi) + 20.0 * Math.sin(2.0 * x * this.pi)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(x * this.pi) + 40.0 * Math.sin(x / 3.0 * this.pi)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(x / 12.0 * this.pi) + 300.0 * Math.sin(x / 30.0 * this.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-sacle=1,user-scalable=no" />
<style>
body{
margin: 0;
padding: 0
}
.map{
width: 100%;
height: 300px;
}
</style>
</head>
<body>
<div id="map" class="map"></div>
<hr>
<input type="text" name="" placeholder="请输入地址">
<div class="listAddr"></div>
<script src="http://webapi.amap.com/maps?v=1.3&key=你的key"></script>
<script src="./js/map.js"></script>
<script>
const newMap = new Maps({
dom:"map",
setting:{
center: [116.397428, 39.90923],//设置中心点
zoom: 16,//设置地图缩放级别PC取值范围为3-18,移动端为3-19
dragEnable:false,//设置地图是否可以鼠标拖拽
zoomEnable:false//设置地图是否可缩放
}
})
newMap.convertAdress({
posi:[104.0650229027,30.5446948650]
}).then(function (data) {
console.log(data);
});
let res = newMap.earthTransformMars(30.5446948650,104.0650229027);
console.log(res);
</script>
</body>
</html>

如果对您有所帮助或者对博主有更多的话说,欢迎你去我的GitHub留下一个您的start和issues

前往LEE的github给他一个Start鼓励一下吧

原创转载请标注出处:https://leehongqiang.github.io