真实标准坐标即我们获取到的坐标,真实坐标经经过国家测绘局进行加密后形成火星坐标(WGS-84),百度地图坐标,是在火星坐标的基础上再进行一次加密,形成了百度地图上的坐标,因此,直接将标准地球坐标显示在百度地图上是会有几百米的偏差的。按照此原理,标准GPS坐标经过两步的转换可得到百度坐标,百度在线的API可以实现,但有些项目环境是不需要百度在线API的,所以需要我们自己转换。
后台转换算法如下:
/**
* pi: 圆周率。
a: 卫星椭球坐标投影到平面地图坐标系的投影因子。
ee: 椭球的偏心率。
x_pi: 圆周率转换量。
transformLat(lat, lon): 输入:横纵坐标,输出:转换后的横坐标。
transformLon(lat, lon): 输入:横纵坐标,输出:转换后的纵坐标。
wgs2gcj(lat, lon): WGS坐标转换为GCJ坐标。
gcj2bd(lat, lon): GCJ坐标转换为百度坐标。
*/
public class MapTest {
static double pi = 3.14159265358979324;
static double a = 6378245.0;
static double ee = 0.00669342162296594323;
public final static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
public double[] wgs2bd(double lat, double lon) {
double[] wgs2gcj = wgs2gcj(lat, lon);
double[] gcj2bd = gcj2bd(wgs2gcj[0], wgs2gcj[1]);
return gcj2bd;
}
public static double[] gcj2bd(double lat, double lon) {
double x = lon, y = lat;
double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
double bd_lon = z * Math.cos(theta) + 0.0065;
double bd_lat = z * Math.sin(theta) + 0.006;
return new double[]{bd_lat, bd_lon};
}
public static double[] bd2gcj(double lat, double lon) {
double x = lon - 0.0065, y = lat - 0.006;
double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
double gg_lon = z * Math.cos(theta);
double gg_lat = z * Math.sin(theta);
return new double[]{gg_lat, gg_lon};
}
public static double[] wgs2gcj(double lat, double lon) {
double dLat = transformLat(lon - 105.0, lat - 35.0);
double dLon = transformLon(lon - 105.0, lat - 35.0);
double radLat = lat / 180.0 * pi;
double magic = Math.sin(radLat);
magic = 1 - ee * magic * magic;
double sqrtMagic = Math.sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
double mgLat = lat + dLat;
double mgLon = lon + dLon;
double[] loc = {mgLat, mgLon};
return loc;
}
private static double transformLat(double lat, double lon) {
double ret = -100.0 + 2.0 * lat + 3.0 * lon + 0.2 * lon * lon + 0.1 * lat * lon + 0.2 * Math.sqrt(Math.abs(lat));
ret += (20.0 * Math.sin(6.0 * lat * pi) + 20.0 * Math.sin(2.0 * lat * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(lon * pi) + 40.0 * Math.sin(lon / 3.0 * pi)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(lon / 12.0 * pi) + 320 * Math.sin(lon * pi / 30.0)) * 2.0 / 3.0;
return ret;
}
private static double transformLon(double lat, double lon) {
double ret = 300.0 + lat + 2.0 * lon + 0.1 * lat * lat + 0.1 * lat * lon + 0.1 * Math.sqrt(Math.abs(lat));
ret += (20.0 * Math.sin(6.0 * lat * pi) + 20.0 * Math.sin(2.0 * lat * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(lat * pi) + 40.0 * Math.sin(lat / 3.0 * pi)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(lat / 12.0 * pi) + 300.0 * Math.sin(lat / 30.0 * pi)) * 2.0 / 3.0;
return ret;
}
}
public static void main(String args[]) {
MapTest mapTest = new MapTest();
double a[] = mapTest.wgs2bd(23.005633,113.073192);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}public static void main(String args[]) {
MapTest mapTest = new MapTest();
double a[] = mapTest.wgs2bd(23.005633,113.073192);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
在线API的转换:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;}
#l-map{height:100%;width:78%;float:left;border-right:2px solid #bcbcbc;}
#r-result{height:100%;width:20%;float:left;}
</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.5&ak=9fb983ecd9b505f8fedcc9ab07c65e3e"></script>
<script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/demo/convertor.js"></script>
<title>GPS转百度</title>
</head>
<body>
<div id="allmap"></div>
</body>
</html>
<script type="text/javascript">
//GPS坐标
var xx = 113.073192;
var yy = 23.005633;
var gpsPoint = new BMap.Point(xx,yy);
//地图初始化
var bm = new BMap.Map("allmap");
bm.centerAndZoom(gpsPoint, 15);
bm.addControl(new BMap.NavigationControl());
//添加谷歌marker和label
var markergps = new BMap.Marker(gpsPoint);
bm.addOverlay(markergps); //添加GPS标注
var labelgps = new BMap.Label("我是GPS标注哦",{offset:new BMap.Size(20,-10)});
markergps.setLabel(labelgps); //添加GPS标注
var xx1 = 113.08517146657847;
var yy1 = 23.00891816682947;
var gpsPoint1 = new BMap.Point(xx1,yy1);
var markergps1 = new BMap.Marker(gpsPoint1);
bm.addOverlay(markergps1); //添加GPS标注
var labelgps1 = new BMap.Label("我是离线标注哦",{offset:new BMap.Size(30,-10)});
markergps1.setLabel(labelgps1); //添加GPS标注
//坐标转换完之后的回调函数
translateCallback = function (point){
var marker = new BMap.Marker(point);
bm.addOverlay(marker);
var label = new BMap.Label("我是百度标注哦",{offset:new BMap.Size(20,-10)});
marker.setLabel(label); //添加百度label
bm.setCenter(point);
alert("转化为百度坐标为:"+point.lng + "," + point.lat);
}
setTimeout(function(){
BMap.Convertor.translate(gpsPoint,0,translateCallback); //真实经纬度转成百度坐标
}, 2000);
</script>