百度地图API坐标批量转换
2025-03-26 14:27:06 来源:华启智能
百度地图API接口使用的坐标系是对地理位置进行了BD-09二次加密,和国际经纬度坐标标准WGS-84是不同的,因此需要进行转换才能在地图上显示正确的位置。

百度的API服务端转换方法为:
http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=longitude&y=latitude
其中:
from: 来源坐标系 (0表示原始GPS坐标,2表示Google坐标)
to: 转换后的坐标 (4就是百度自己啦,好像这个必须是4才行)
x: 经度
y: 纬度
返回的结果是一个json字符串:
{"error":0,"x":"MTIxLjUwMDIyODIxNDk2","y":"MzEuMjM1ODUwMjYwMTE3"}
其中:
error:是结果是否出错标志位,"0"表示OK
x: 百度坐标系的经度(Base64加密)
y: 百度坐标系的纬度(Base64加密)
百度地图客户端坐标转换接口如下:
var gpsPoint = new BMapGL.Point(经度,纬度); ( GPS坐标)
var gpsPoint = new BMapGL.Point(经度,纬度); ( GPS坐标)
var convertor = new BMapGL.Convertor();
//真实经纬度转成百度坐标
//真实经纬度转成百度坐标
convertor.translate(gpsPoint, COORDINATES_WGS84, COORDINATES_BD09, function (data){
if(data.status === 0) {
}
else if(data.status === 25) {
}
});
}
其中gpsPoint, var gpsPoint = new BMap.Point(经度,纬度); ( GPS坐标) 0:代表GPS,也可以是2:google坐标 translateCallback:回调函数
/**
* 坐标常量说明:
* COORDINATES_WGS84 = 1, WGS84坐标
* COORDINATES_WGS84_MC = 2, WGS84的平面墨卡托坐标
* COORDINATES_GCJ02 = 3,GCJ02坐标
* COORDINATES_GCJ02_MC = 4, GCJ02的平面墨卡托坐标
* COORDINATES_BD09 = 5, 百度bd09经纬度坐标
* COORDINATES_BD09_MC = 6,百度bd09墨卡托坐标
* COORDINATES_MAPBAR = 7,mapbar地图坐标
* COORDINATES_51 = 8,51地图坐标
*/
当需要进行批量转换时,gpsPoint使用数组即可。在调试时发现回调函数返回的值0代表成功,我碰到返回25,查资料发现原因是个数非法,超过限制,百度API一次最多只支持10个点坐标转换,超过10个是返回错误码25.*/
状态码说明:
当我要转换的点数较多时,解决方法是以每十个点为一组,然后分批进行转换。这个接口是异步执行的,所以结果中的顺序可能会乱。这个需要注意。当循环调用该接口时需要做识别。
另外查资料发现有网友提供的本地实现的方法,转载如下:
这个本地算法不知道是不是官方的,准确性没有保证。
参考资料:
| 状态码 | 含义 |
| 0 | 正常 |
| 1 | 内部错误 |
| 21 | from非法 |
| 22 | to非法 |
| 24 | coords格式非法 |
| 25 | coords个数非法,超过限制 |
当我要转换的点数较多时,解决方法是以每十个点为一组,然后分批进行转换。这个接口是异步执行的,所以结果中的顺序可能会乱。这个需要注意。当循环调用该接口时需要做识别。
另外查资料发现有网友提供的本地实现的方法,转载如下:
/**
* 坐标系转换函数
* WGS->GCJ
* WGS->BD09
* GCJ->BD09
*/
function Convertor(ak) {
this.stepCount = 100;
this.pointCount = [];
this.Result = [];
this.NoisIndex = [];
this.Time = new Date();
this.AK = ak;
this.M_PI = 3.14159265358979324;
this.A = 6378245.0;
this.EE = 0.00669342162296594323;
this.X_PI = this.M_PI * 3000.0 / 180.0;
}
Convertor.prototype.outofChine = function(p) {
if (p.lng < 72.004 || p.lng > 137.8347) {
return true;
}
if (p.lat < 0.8293 || p.lat > 55.8271) {
return true;
}
return false;
}
;
Convertor.prototype.WGS2GCJ_lat = function(x, y) {
var ret1 = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
ret1 += (20.0 * Math.sin(6.0 * x * this.M_PI) + 20.0 * Math.sin(2.0 * x * this.M_PI)) * 2.0 / 3.0;
ret1 += (20.0 * Math.sin(y * this.M_PI) + 40.0 * Math.sin(y / 3.0 * this.M_PI)) * 2.0 / 3.0;
ret1 += (160.0 * Math.sin(y / 12.0 * this.M_PI) + 320 * Math.sin(y * this.M_PI / 30.0)) * 2.0 / 3.0;
return ret1;
}
;
Convertor.prototype.WGS2GCJ_lng = function(x, y) {
var ret2 = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
ret2 += (20.0 * Math.sin(6.0 * x * this.M_PI) + 20.0 * Math.sin(2.0 * x * this.M_PI)) * 2.0 / 3.0;
ret2 += (20.0 * Math.sin(x * this.M_PI) + 40.0 * Math.sin(x / 3.0 * this.M_PI)) * 2.0 / 3.0;
ret2 += (150.0 * Math.sin(x / 12.0 * this.M_PI) + 300.0 * Math.sin(x / 30.0 * this.M_PI)) * 2.0 / 3.0;
return ret2;
}
;
Convertor.prototype.WGS2GCJ = function(poi) {
if (this.outofChine(poi)) {
return;
}
var poi2 = {};
var dLat = this.WGS2GCJ_lat(poi.lng - 105.0, poi.lat - 35.0);
var dLon = this.WGS2GCJ_lng(poi.lng - 105.0, poi.lat - 35.0);
var radLat = poi.lat / 180.0 * this.M_PI;
var magic = Math.sin(radLat);
magic = 1 - this.EE * magic * magic;
var sqrtMagic = Math.sqrt(magic);
dLat = (dLat * 180.0) / ((this.A * (1 - this.EE)) / (magic * sqrtMagic) * this.M_PI);
dLon = (dLon * 180.0) / (this.A / sqrtMagic * Math.cos(radLat) * this.M_PI);
poi2.lat = poi.lat + dLat;
poi2.lng = poi.lng + dLon;
return poi2;
}
;
Convertor.prototype.GCJ2BD09 = function(poi) {
var poi2 = {};
var x = poi.lng
, y = poi.lat;
var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * this.X_PI);
var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * this.X_PI);
poi2.lng = z * Math.cos(theta) + 0.0065;
poi2.lat = z * Math.sin(theta) + 0.006;
return poi2;
}
;
/**
* WGS->百度坐标系
*/
Convertor.prototype.WGS2BD09 = function(poi) {
//WGS->GCJ
var poi2 = this.WGS2GCJ(poi);
if (typeof poi2 === "undefined") {
return;
}
//GCJ->百度坐标系
return this.GCJ2BD09(poi2);
}
//TEST
var c=new Convertor();
var r1=c.WGS2BD09({lng:123,lat:38});
console.log(r1);
参考资料:
https://blog.csdn.net/itmyhome1990/article/details/50426928
https://www.cnblogs.com/wongjzzz/articles/16229921.html
https://www.cnblogs.com/moguzi12345/p/7575519.html
https://www.cnblogs.com/limmy/p/7205115.html
https://bbs.csdn.net/topics/392006345

