通过android手机内置GPS获取平面直角坐标和高斯坐标的原理(附代码)

在部队军事地形学和地方测绘工作中,常常需要知道当前位置的平面直角坐标,现在仅仅通过一个带GPS的安卓手机就可以实现。以后在外出找点时,就靠它了。

先看效果:

 下载地址:

1:5万版apk文件下载

1:2.5万版apk文件下载

程序源码下载

 

//首先通过android api函数获取GPS地理坐标即经纬度

LocationManager lm;
lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
if(!lm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER))
{
  Toast.makeText(MainActivity.this, "GPS设备,未开启,请打开位置和安全设置,打开GPS卫星",Toast.LENGTH_LONG).show();
  return ;
}
String bestProvider=lm.getBestProvider(getCriteria(), true);
Location l=lm.getLastKnownLocation(bestProvider);
lm.requestLocationUpdates(bestProvider,5000,8, listener);

double x,y;
  // 
x=location.getLatitude();
  //纬度
 y=location.getLongitude();//经度

 接着进行经纬度转化为高斯坐标,网上此类函数很多,不过大都不够准确,下面是最准确的一种。

//经纬度转换平面坐标
    double[] GaussProjCal(double longitude, double latitude) 
    {
        int ProjNo=0; int ZoneWide; ////带宽
        double longitude1,latitude1, longitude0,latitude0, X0,Y0, xval,yval;
        double a,f, e2,ee, NN, T,C,A, M, iPI;
        iPI = 0.0174532925199433; ////3.1415926535898/180.0;
        ZoneWide = 6; ////6度带宽
//        a=6378245.0; f=1.0/298.3; //54年北京坐标系参数
        ////a=6378140.0; f=1/298.257; //80年西安坐标系参数
        a=m_a;f=m_f;
        if(longitude % ZoneWide==0)
            ProjNo = (int)(longitude / ZoneWide)-1;
        else
            ProjNo = (int)(longitude / ZoneWide);
        longitude0 = ProjNo * ZoneWide + ZoneWide / 2;
        longitude0 = longitude0 * iPI ;
        latitude0=0;
        longitude1 = longitude * iPI ; //经度转换为弧度
        latitude1 = latitude * iPI ; //纬度转换为弧度
        e2=2*f-f*f;
        ee=e2*(1.0-e2);
        NN=a/Math.sqrt(1.0-e2*Math.sin(latitude1)*Math.sin(latitude1));
        T=Math.tan(latitude1)*Math.tan(latitude1);
        C=ee*Math.cos(latitude1)*Math.cos(latitude1);
        A=(longitude1-longitude0)*Math.cos(latitude1);
        M=a*((1-e2/4-3*e2*e2/64-5*e2*e2*e2/256)*latitude1-(3*e2/8+3*e2*e2/32+45*e2*e2
        *e2/1024)*Math.sin(2*latitude1)
        +(15*e2*e2/256+45*e2*e2*e2/1024)*Math.sin(4*latitude1)-(35*e2*e2*e2/3072)*Math.sin(6*latitude1));
        xval = NN*(A+(1-T+C)*A*A*A/6+(5-18*T+T*T+72*C-58*ee)*A*A*A*A*A/120);
        yval = M+NN*Math.tan(latitude1)*(A*A/2+(5-T+9*C+4*C*C)*A*A*A*A/24
        +(61-58*T+T*T+600*C-330*ee)*A*A*A*A*A*A/720);
        X0 = 500000L;
        Y0 = 0;
        xval = xval+X0; yval = yval+Y0;
        double X = xval;
        double Y = yval;
        double long_laNum[]=new double[3];
        long_laNum[0]=(X);//经度
        long_laNum[1]=(Y);//纬度
        long_laNum[2]=ProjNo+1;//纬度
        return long_laNum;
    }

以下是高斯坐标转化为经纬度的函数

double[] GaussProjInvCal(double X, double Y)
	{
		int ProjNo; int ZoneWide; ////带宽
		double longitude1,latitude1, longitude0,latitude0, X0,Y0, xval,yval;
		double e1,e2,f,a, ee, NN, T,C, M, D,R,u,fai, iPI;
		iPI = 0.0174532925199433; ////3.1415926535898/180.0;
//		a = 6378245.0; 
//		f = 1.0/298.3; //54年北京坐标系参数
		a=m_a;f=m_f;
		////a=6378140.0; f=1/298.257; //80年西安坐标系参数
		ZoneWide = 6; ////6度带宽
		ProjNo = (int)(X/1000000L) ; //查找带号
		longitude0 = (ProjNo-1) * ZoneWide + ZoneWide / 2;
		longitude0 = longitude0 * iPI ; //中央经线
		X0 = ProjNo*1000000L+500000L;
		Y0 = 0;
		xval = X-X0; yval = Y-Y0; //带内大地坐标
		e2 = 2*f-f*f;
		e1 = (1.0-Math.sqrt(1-e2))/(1.0+Math.sqrt(1-e2));
		ee = e2/(1-e2);
		M = yval;
		u = M/(a*(1-e2/4-3*e2*e2/64-5*e2*e2*e2/256));
		fai = u+(3*e1/2-27*e1*e1*e1/32)*Math.sin(2*u)+(21*e1*e1/16-55*e1*e1*e1*e1/32)*Math.sin(
		4*u)
		+(151*e1*e1*e1/96)*Math.sin(6*u)+(1097*e1*e1*e1*e1/512)*Math.sin(8*u);
		C = ee*Math.cos(fai)*Math.cos(fai);
		T = Math.tan(fai)*Math.tan(fai);
		NN = a/Math.sqrt(1.0-e2*Math.sin(fai)*Math.sin(fai));
		R = a*(1-e2)/Math.sqrt((1-e2*Math.sin(fai)*Math.sin(fai))*(1-e2*Math.sin(fai)*Math.sin(fai))*(1-e2*Math.sin
		(fai)*Math.sin(fai)));
		D = xval/NN;
		//计算经度(Longitude) 纬度(Latitude)
		longitude1 = longitude0+(D-(1+2*T+C)*D*D*D/6+(5-2*C+28*T-3*C*C+8*ee+24*T*T)*D
		*D*D*D*D/120)/Math.cos(fai);
		latitude1 = fai -(NN*Math.tan(fai)/R)*(D*D/2-(5+3*T+10*C-4*C*C-9*ee)*D*D*D*D/24
		+(61+90*T+298*C+45*T*T-256*ee-3*C*C)*D*D*D*D*D*D/720);
		//转换为度 DD
		double longitude = longitude1 / iPI;
		double latitude = latitude1 / iPI;
		double long_la[]=new double[2];
		long_la[0]=longitude;
		long_la[1]=latitude;
		return long_la;
	}

 以下功能是通过经纬度计算1:5万地形图的图幅编号。

public String changeWwwd(int wdu,int wfen,int wmiao,int jdu,int jfen,int jmiao)
	{
		double mywd;//39.48
		double myjd;//121.42
		mywd=wdu*60+wfen+wmiao/60;
		myjd=jdu*60+jfen+jmiao/60;
		int a=wdu/4+1;
		int b=jdu/6+31;
		int c;
		int swwd[]=new int[12];
		int swjd[]=new int[12];
		int i,j;
		int linex=0,liney=0;
		for(i=0;i<12;i++)
		{
			swwd[i]=a*4*60-20*(i+1);
		}
		for(i=0;i<12;i++)
		{
			if(swwd[i]<mywd)
			{	
				linex=i;
				break;
			}
		}
		for(j=0;j<12;j++)
		{
			swjd[j]=(b-31)*6*60+30*(j+1);
		}
		for(j=0;j<12;j++)
		{
			if(swjd[j]>myjd)
			{
				liney=j+1;
				break;
			}
		}
		c=linex*12+liney;
		
		int xtag=0,ytag=0;
		
		if(mywd>swwd[i]+10)
			xtag=1;
		else 
			xtag=2;
		
		if(myjd<swjd[j]-15)
			ytag=1;
		else 
			ytag=2;
		String wwwd="";
		switch (xtag)
		{
		case 1:
			if(ytag==1){
				wwwd="甲";
			}
			else {
				wwwd="乙";
			}
			break;
		case 2:
			if(ytag==1){
				wwwd="丙";
			}
			else {
				wwwd="丁";
			}
			break;
		default:
			break;
		}
		wwwd=a+"-"+b+"-"+c+"-"+wwwd;
		return wwwd;
	}
posted @ 2013-05-19 23:37  再见,青春  阅读(4978)  评论(0编辑  收藏  举报