前面写了一篇关于 通过偏移将GPS坐标转换成百度坐标--js实现 的,连接:http://blog.csdn.net/slamdunk006/article/details/50170653
现在在补一刀,哈哈,关于服务端实现的。
服务端实现,跟js端实现大同小异,没啥子区别,只是封装不同罢了。在服务端实现的好处在于,数据展示的时候,从耗时的角度考虑,优势还是挺大的,毕竟数据量都是比较庞大的,如果每次都是客户端转换一遍,这就属于做无用功了。哈哈~~~,说多了。开始放大招~~~~~~~~~~~~~~
先来个gps转火星的封装类
public class GpsConverter
{
GpsConverter me;
double casm_rr = 0;
double casm_t1 = 0;
double casm_t2 = 0;
double casm_x1 = 0;
double casm_y1 = 0;
double casm_x2 = 0;
double casm_y2 = 0;
double casm_f = 0;
public GpsConverter()
{
this.me = this;
this.casm_rr = 0;
this.casm_t1 = 0;
this.casm_t2 = 0;
this.casm_x1 = 0;
this.casm_y1 = 0;
this.casm_x2 = 0;
this.casm_y2 = 0;
this.casm_f = 0;
}
protected double yj_sin2(double x)
{
double tt;
double ss;
double ff;
double s2;
int cc;
ff = 0;
if (x < 0)
{
x = -x;
ff = 1;
}
cc = (int)(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;
}
protected double Transform_yj5(double x, double y)
{
double 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 * me.yj_sin2(18.849555921538764 * x) + 20 * me.yj_sin2(6.283185307179588 * x)) * 0.6667;
tt = tt + (20 * me.yj_sin2(3.141592653589794 * x) + 40 * me.yj_sin2(1.047197551196598 * x)) * 0.6667;
tt = tt + (150 * me.yj_sin2(0.2617993877991495 * x) + 300 * me.yj_sin2(0.1047197551196598 * x)) * 0.6667;
return tt;
}
protected double Transform_yjy5(double x, double y)
{
double 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 * me.yj_sin2(18.849555921538764 * x) + 20 * me.yj_sin2(6.283185307179588 * x)) * 0.6667;
tt = tt + (20 * me.yj_sin2(3.141592653589794 * y) + 40 * me.yj_sin2(1.047197551196598 * y)) * 0.6667;
tt = tt + (160 * me.yj_sin2(0.2617993877991495 * y) + 320 * me.yj_sin2(0.1047197551196598 * y)) * 0.6667;
return tt;
}
protected double Transform_jy5(double x, double xx)
{
double n;
double a;
double e;
a = 6378245;
e = 0.00669342;
n = Math.Sqrt(1 - e * me.yj_sin2(x * 0.0174532925199433) * me.yj_sin2(x * 0.0174532925199433));
n = (xx * 180) / (a / n * Math.Cos(x * 0.0174532925199433) * 3.1415926);
return n;
}
protected double Transform_jyj5(double x, double yy)
{
double m;
double a;
double e;
double mm;
a = 6378245;
e = 0.00669342;
mm = 1 - e * me.yj_sin2(x * 0.0174532925199433) * me.yj_sin2(x * 0.0174532925199433);
m = (a * (1 - e)) / (mm * Math.Sqrt(mm));
return (yy * 180) / (m * 3.1415926);
}
protected int r_yj()
{
int casm_a = 314159269;
int casm_c = 453806245;
return 0;
}
protected double random_yj()
{
double t;
double casm_a = 314159269;
double casm_c = 453806245;
me.casm_rr = casm_a * me.casm_rr + casm_c;
t = (int)(me.casm_rr / 2);
me.casm_rr = me.casm_rr - t * 2;
me.casm_rr = me.casm_rr / 2;
return (me.casm_rr);
}
protected void IniCasm(double w_time, double w_lng, double w_lat)
{
double tt;
me.casm_t1 = w_time;
me.casm_t2 = w_time;
tt = (int)(w_time / 0.357);
me.casm_rr = w_time - tt * 0.357;
if (w_time == 0)
me.casm_rr = 0.3;
me.casm_x1 = w_lng;
me.casm_y1 = w_lat;
me.casm_x2 = w_lng;
me.casm_y2 = w_lat;
me.casm_f = 3;
}
protected Point wgtochina_lb(int wg_flag, int wg_lng, int wg_lat, int wg_heit, int wg_week, int wg_time)
{
double x_add;
double y_add;
double h_add;
double x_l;
double y_l;
double casm_v;
double t1_t2;
double x1_x2;
double y1_y2;
Point 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;
}
if (wg_flag == 0)
{
me.IniCasm(wg_time, wg_lng, wg_lat);
point = new Point();
point.setLongitude(wg_lng);
point.setLatitude(wg_lat);
return point;
}
me.casm_t2 = wg_time;
t1_t2 = (me.casm_t2 - me.casm_t1) / 1000.0;
if (t1_t2 <= 0)
{
me.casm_t1 = me.casm_t2;
me.casm_f = me.casm_f + 1;
me.casm_x1 = me.casm_x2;
me.casm_f = me.casm_f + 1;
me.casm_y1 = me.casm_y2;
me.casm_f = me.casm_f + 1;
}
else
{
if (t1_t2 > 120)
{
if (me.casm_f == 3)
{
me.casm_f = 0;
me.casm_x2 = wg_lng;
me.casm_y2 = wg_lat;
x1_x2 = me.casm_x2 - me.casm_x1;
y1_y2 = me.casm_y2 - me.casm_y1;
casm_v = Math.Sqrt(x1_x2 * x1_x2 + y1_y2 * y1_y2) / t1_t2;
if (casm_v > 3185)
{
return (point);
}
}
me.casm_t1 = me.casm_t2;
me.casm_f = me.casm_f + 1;
me.casm_x1 = me.casm_x2;
me.casm_f = me.casm_f + 1;
me.casm_y1 = me.casm_y2;
me.casm_f = me.casm_f + 1;
}
}
x_add = me.Transform_yj5(x_l - 105, y_l - 35);
y_add = me.Transform_yjy5(x_l - 105, y_l - 35);
h_add = wg_heit;
x_add = x_add + h_add * 0.001 + me.yj_sin2(wg_time * 0.0174532925199433) + me.random_yj();
y_add = y_add + h_add * 0.001 + me.yj_sin2(wg_time * 0.0174532925199433) + me.random_yj();
point = new Point();
point.setLongitude(((x_l + me.Transform_jy5(y_l, x_add)) * 3686400));
point.setLatitude(((y_l + me.Transform_jyj5(y_l, y_add)) * 3686400));
return point;
}
public Point getEncryPoint(double x, double y)
{
Point point = new Point();
double x1, tempx;
double y1, tempy;
x1 = x * 3686400.0;
y1 = y * 3686400.0;
double gpsWeek = 0;
double gpsWeekTime = 0;
double gpsHeight = 0;
point = me.wgtochina_lb(1, (int)(x1), (int)(y1), (int)(gpsHeight), (int)(gpsWeek), (int)(gpsWeekTime));
tempx = point.getLongitude();
tempy = point.getLatitude();
tempx = tempx / 3686400.0;
tempy = tempy / 3686400.0;
point = new Point();
point.setLongitude(tempx);
point.setLatitude(tempy);
return point;
}
}
-------------------------------------------------------分割线,仅仅是分割线,哈哈------------------------------------------------
两个函数,随便怎么处理,随便怎么存储,我一般都是放在静态类中,便于全局调用,拙见拙见~~~~~~
///
/// GCJ-02转百度坐标
///
///
///
public static Point bd_encrypt(double gg_lat, double gg_lon)
{
Point p = new Point();
double x = gg_lon;
double y = gg_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;
p.setLatitude(bd_lat);
p.setLongitude(bd_lon);
return p;
}
///
/// 百度坐标转GCJ-02坐标
///
///
///
public static Point bd_decrypt(double bd_lat, double bd_lon)
{
Point p=new Point();
double x = bd_lon - 0.0065, y = bd_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);
p.setLatitude(gg_lat);
p.setLongitude(gg_lon);
return p;
}
-------------------------------------------------------分割线,仅仅是分割线,哈哈------------------------------------------------
调用的时候很容易,如下所示
//转换成GCJ-02坐标系
GpsConverter gcc = new GpsConverter();
Point gcj_pt = gcc.getEncryPoint(lng, lat);
//转换成BD-09坐标系
Point bd_pt = Pub.bd_encrypt(gcj_pt.getLatitude(), gcj_pt.getLongitude());
posInfo.BDLat = bd_pt.getLatitude();
posInfo.BDLng = bd_pt.getLongitude();
-------------------------------------------------------分割线,仅仅是分割线,哈哈------------------------------------------------
至于后面的事情,我就不说啦,该存储的存储,该处理的处理。哈哈~~~~~~~~~~~~~~~~~~~~~~