之前做GPS数据采集,已经存储在后台数据中,但是后来需求需要用到百度地图作为数据显示(谷歌的大家都懂得),刚开始还是用的百度的转换接口,转换肯定没有问题,百度单次转换接口为:BMap.Convertor.translate(gpsPoint,0,translateCallback); //真实经纬度转成百度坐标
据说还支持批量转换,但是有请求限制,每次50个点吧。如果是这样的话,太影响效率了额,而且请求是异步的,返回的数据跟之前的数据顺序完全不同,所以对于轨迹回放来说,万全是晴天霹雳啊。
铺垫了这么多,其实就是想说,针对gps坐标系的,要么在入库之前就做GPS坐标转换,要么就在数据显示前做GPS坐标系转换(非要矫情的说:可以选用GPS坐标系的地图或服务啥的,那就不用往下看了。哈哈),这次先给个用js转换的,下次再给个服务端的,这样客户端、服务端都有了。哈哈~~~~
PS:js实现GPS坐标转百度坐标,通过偏移量实现
/**********************************************************/
// 全局参数
var Json =[];//接受转换了的百度坐标
var casm_rr = 0;
var casm_t1 = 0;
var casm_t2 = 0;
var casm_x1 = 0;
var casm_y1 = 0;
var casm_x2 = 0;
var casm_y2 = 0;
var casm_f = 0;
var xxx_pi = 3.14159265358979324 * 3000.0 / 180.0;
/**********************************************************/
// 在数据封装好后,载入getpoint(); 即可
function Transform_yj5(x,y) {
var tt;
tt = 300 + 1 * x + 2 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.sqrt(x * x));
tt = tt + (20 * yj_sin2(18.849555921538764 * x) + 20 * yj_sin2(6.283185307179588 * x)) * 0.6667;
tt = tt + (20 * yj_sin2(3.141592653589794 * x) + 40 * yj_sin2(1.047197551196598 * x)) * 0.6667;
tt = tt + (150 * yj_sin2(0.2617993877991495 * x) + 300 * yj_sin2(0.1047197551196598 * x)) * 0.6667;
return tt;
}
function Transform_yjy5(x, y) {
var tt;
tt = -100 + 2 * x + 3 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.sqrt(x * x));
tt = tt + (20 * yj_sin2(18.849555921538764 * x) + 20 * yj_sin2(6.283185307179588 * x)) * 0.6667;
tt = tt + (20 * yj_sin2(3.141592653589794 * y) + 40 * yj_sin2(1.047197551196598 * y)) * 0.6667;
tt = tt + (160 * yj_sin2(0.2617993877991495 * y) + 320 * yj_sin2(0.1047197551196598 * y)) * 0.6667;
return tt;
}
function random_yj()
{
var t;
var casm_a = 314159269;
var casm_c = 453806245;
casm_rr = casm_a * casm_rr + casm_c;
t = (casm_rr / 2);
casm_rr = casm_rr - t * 2;
casm_rr = casm_rr / 2;
return (casm_rr);
}
function Transform_jy5(x, xx)
{
var n;
var a;
var e;
a = 6378245;
e = 0.00669342;
n = Math.sqrt(1 - e * yj_sin2(x * 0.0174532925199433) * yj_sin2(x * 0.0174532925199433));
n = (xx * 180) / (a / n * Math.cos(x * 0.0174532925199433) * 3.1415926);
return n;
}
function Transform_jyj5(x, yy)
{
var m;
var a;
var e;
var mm;
a = 6378245;
e = 0.00669342;
mm = 1 - e * yj_sin2(x * 0.0174532925199433) * yj_sin2(x * 0.0174532925199433);
m = (a * (1 - e)) / (mm * Math.sqrt(mm));
return (yy * 180) / (m * 3.1415926);
}
function yj_sin2(x)
{
var tt;
var ss;
var ff;
var s2;
var cc;
ff = 0;
if (x < 0)
{
x = -x;
ff = 1;
}
cc = (x / 6.28318530717959);
tt = x - cc * 6.28318530717959;
if (tt > 3.1415926535897932)
{
tt = tt - 3.1415926535897932;
if (ff == 1)
{
ff = 0;
}
else if (ff == 0)
{
ff = 1;
}
}
x = tt;
ss = x;
s2 = x;
tt = tt * tt;
s2 = s2 * tt;
ss = ss - s2 * 0.166666666666667;
s2 = s2 * tt;
ss = ss + s2 * 8.33333333333333E-03;
s2 = s2 * tt;
ss = ss - s2 * 1.98412698412698E-04;
s2 = s2 * tt;
ss = ss + s2 * 2.75573192239859E-06;
s2 = s2 * tt;
ss = ss - s2 * 2.50521083854417E-08;
if (ff == 1)
{
ss = -ss;
}
return ss;
}
function wgtochina_lb(wg_flag, wg_lng, wg_lat, wg_heit, wg_week, wg_time) {
var x_add;
var y_add;
var h_add;
var x_l;
var y_l;
var casm_v;
var t1_t2;
var x1_x2;
var y1_y2;
var point = null;
if (wg_heit > 5000)
{
return point;
}
x_l = wg_lng;
x_l = x_l / 3686400.0;
y_l = wg_lat;
y_l = y_l / 3686400.0;
if (x_l < 72.004)
{
return point;
}
if (x_l > 137.8347)
{
return point;
}
if (y_l < 0.8293)
{
return point;
}
if (y_l > 55.8271)
{
return point;
}
casm_t2 = wg_time;
t1_t2 = (casm_t2 - casm_t1) / 1000.0;
if (t1_t2 <= 0)
{
casm_t1 = casm_t2;
casm_f = casm_f + 1;
casm_x1 = casm_x2;
casm_f = casm_f + 1;
casm_y1 = casm_y2;
casm_f = casm_f + 1;
}
else
{
if (t1_t2 > 120)
{
if (casm_f == 3)
{
casm_f = 0;
casm_x2 = wg_lng;
casm_y2 = wg_lat;
x1_x2 = casm_x2 - casm_x1;
y1_y2 = casm_y2 - casm_y1;
casm_v = Math.sqrt(x1_x2 * x1_x2 + y1_y2 * y1_y2) / t1_t2;
if (casm_v > 3185)
{
return (point);
}
}
casm_t1 = casm_t2;
casm_f = casm_f + 1;
casm_x1 = casm_x2;
casm_f = casm_f + 1;
casm_y1 = casm_y2;
casm_f = casm_f + 1;
}
}
x_add = Transform_yj5(x_l - 105, y_l - 35);
y_add = Transform_yjy5(x_l - 105, y_l - 35);
h_add = wg_heit;
x_add = x_add + h_add * 0.001 + yj_sin2(wg_time * 0.0174532925199433) + random_yj();
y_add = y_add + h_add * 0.001 + yj_sin2(wg_time * 0.0174532925199433) + random_yj();
point = new BMap.Point(((x_l + Transform_jy5(y_l, x_add)) * 3686400), ((y_l + Transform_jyj5(y_l, y_add)) * 3686400));
return point;
}
function bd_encrypt(gg_lat, gg_lon)
{
var x = gg_lon;
var y = gg_lat;
var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * xxx_pi);
var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * xxx_pi);
var bd_lon = z * Math.cos(theta) + 0.0065;
var bd_lat = z * Math.sin(theta) + 0.006;
var point = new BMap.Point(bd_lon, bd_lat);
return point;
}
function getpoint() {
bpoints = [];
Json = []; //接受转换了的百度坐标 初始化全局变量
for (var i = 0; i < pointData.length; i++) {
var gpsPoint = new BMap.Point(pointData[i].jingdu, pointData[i].weidu);
var x = gpsPoint.lng;
var y = gpsPoint.lat;
var x1, tempx;
var y1, tempy;
x1 = x * 3686400.0;
y1 = y * 3686400.0;
var gpsWeek = 0;
var gpsWeekTime = 0;
var gpsHeight = 0;
var point = wgtochina_lb(1, x1, y1, gpsHeight,gpsWeek, gpsWeekTime);
tempx = point.lng;
tempy = point.lat;
tempx = tempx / 3686400.0;
tempy = tempy / 3686400.0;
var pointemp = new BMap.Point(tempx, tempy);
var pointlast= bd_encrypt(pointemp.lat, pointemp.lng);
Json.push(pointlast);
}
var polyline = new BMap.Polyline(Json, { strokeColor: 'blue' });
map.addOverlay(polyline);
}
function getTracePoints(pointData)//从获取的数据中组装一组点
{
var arr = new Array();
for (var i = 0; i < pointData.length; i++) {
arr[i] = new BMap.Point(pointData[i].jingdu, pointData[i].weidu);
}
return arr;
}
function getLandmarkPoints(pointData) {
var arr = new Array();
for (var i = 0; i < pointData.length; i++) {
arr[i] = new Object();
arr[i].lng = pointData[i].jingdu;
arr[i].lat = pointData[i].weidu;
arr[i].html = queryCondition.chepaih + '  ' + pointData[i].caijisj;
arr[i].pauseTime = 1;
}
return arr;
}