1. 程式人生 > >已知地面上兩點經續度求兩點球面距離函式Flex及C

已知地面上兩點經續度求兩點球面距離函式Flex及C

Flex版本

private function getDis(lat1:Number,lng1:Number,lat2:Number,lng2:Number):Number{
				
				var radlat1:Number=lat1*3.1415926/180.0;
				var radlat2:Number=lat2*3.1415926/180.0;
				var radlng1:Number=lng1*3.1415926/180.0;
				var radlng2:Number=lng2*3.1415926/180.0;
				
				var ff:Number=(radlat1+radlat2)/2.0;
				var gg:Number=(radlat1-radlat2)/2.0;
				var ll:Number=(radlng1-radlng2)/2.0;
				
				var ss:Number=Math.pow((Math.sin(gg)),2)*Math.pow((Math.cos(ll)),2)+Math.pow((Math.cos(ff)),2)*Math.pow((Math.sin(ll)),2);
				var cc:Number=Math.pow((Math.cos(gg)),2)*Math.pow((Math.cos(ll)),2)+Math.pow((Math.sin(ff)),2)*Math.pow((Math.sin(ll)),2);
				
				var ww=Math.atan(Math.sqrt(ss/cc));
				var banjin:Number=6378.135;
				var dist:Number=2*ww*banjin;
				var rr:Number=Math.sqrt(ss*cc)/ww;
				var h1:Number=(3*rr-1)/(2*cc);
				var h2:Number=(3*rr+1)/(2*ss);
				var xx:Number=1/298.257223543; //修正率
				var dm:Number;
				if(ww!=0){
					dm =dist*(1+xx*h1*Math.pow((Math.sin(ff)),2)*Math.pow((Math.cos(gg)),2)-xx*h2*Math.pow((Math.cos(ff)),2)*Math.pow((Math.sin(gg)),2));
				}				
				else{
					dm   = 0; 
				}
				return dm;
				
			}

C語言版本

double CJ::GetDis(double lat1, double lng1,   double lat2, double lng2)
{  
 double radlat1 = lat1*3.1415926/180.0;
 double radlat2 =   lat2*3.1415926/180.0;
 double radlng1 =   lng1*3.1415926/180.0;
 double radlng2 = lng2*3.1415926/180.0;  

 double ff = (radlat1+radlat2)/2.0;
 double gg =   (radlat1-radlat2)/2.0;
 double ll = (radlng1-radlng2)/2.0;  
 double ss =   pow((sin(gg)),2)*pow((cos(ll)),2)+pow((cos(ff)),2)*pow((sin(ll)),2);
 double   cc =   pow((cos(gg)),2)*pow((cos(ll)),2)+pow((sin(ff)),2)*pow((sin(ll)),2);
 double   ww = atan(sqrt(ss/cc));  
 double banjin = 6378.135;  //地球半徑km
 double dist =   2*ww*banjin;
 double rr = sqrt(ss*cc)/ww;
 double h1 =   (3*rr-1)/(2*cc);
 double h2 = (3*rr+1)/(2*ss);
 double xx =   1/298.257223543; //修正率
 double dm;  
 if(ww!=0)
 dm =   dist*(1+xx*h1*pow((sin(ff)),2)*pow((cos(gg)),2)-xx*h2*pow((cos(ff)),2)*pow((sin(gg)),2));
 else
 dm   = 0;  
    return dm;
}