K_Reverter的网页开发记录

要么不做,要么就当作艺术品来做!

导航

Google Maps API 2.0解析(6-GLatLng GLatLngBounds GLngSegment GLatSegment经纬度支持的类)

 

//GLatLng对象,一个以经纬度代表的地理点(同时也可以代表地理大小),c参数代表是否进行经纬度强制转化
  function GLatLng(a,b,c)
  {
   
if(!c)
   {
    a
=GetNumberInRange(a,-90,90);
    b
=getLoopNumber(b,-180,180)
   }
   
this.latitude=a;
   
this.longitude=b;
   
this.x=b;
   
this.y=a
  }
  
//转化成字符串
  GLatLng.prototype.toString=function()
  {
   
return"("+this.lat()+""+this.lng()+")"
  };
  
//两个点是否相等
  GLatLng.prototype.equals=function(a)
  {
   
if(!a)return false;
   
return isSameCoordinatePoint(this.lat(),a.lat())&&isSameCoordinatePoint(this.lng(),a.lng())
  };
  
//格式化坐标以得到组成Url的值
  GLatLng.prototype.toUrlValue=function()
  {
   
return formatLatLngNumber(this.lat())+","+formatLatLngNumber(this.lng())
  };
  
//获得纬度
  GLatLng.prototype.lat=function()
  {
   
return this.latitude
  };
  
//获得经度
  GLatLng.prototype.lng=function()
  {
   
return this.longitude
  };
  
//获得纬度的弧度值
  GLatLng.prototype.latRadians=function()
  {
   
return getRadianByDegree(this.latitude)
  };
  
//获得经度的弧度值
  GLatLng.prototype.lngRadians=function()
  {
   
return getRadianByDegree(this.longitude)
  };
  
//计算两点之间的距离
  GLatLng.prototype.distanceFrom=function(a)
  {
   
var b=this.latRadians();
   
var c=a.latRadians();
   
var d=b-c;
   
var e=this.lngRadians()-a.lngRadians();
   
var f=2*Math.asin(Math.sqrt(Math.pow(Math.sin(d/2),2)+Math.cos(b)*Math.cos(c)*Math.pow(Math.sin

(e
/2),2)));
   
return f*6378137
  };
  
//从字符串之中读取出GLatLng点
  GLatLng.fromUrlValue=function(a)
  {
   
var b=a.split(",");
   
return new GLatLng(parseFloat(b[0]),parseFloat(b[1]))
  };
  
//通过弧度坐标创建GLatLng
  GLatLng.fromRadians=function(a,b,c)
  {
   
return new GLatLng(getDegreeByRadian(a),getDegreeByRadian(b),c)
  };
  
//一个通过经纬度指定的地理范围,参数a,b是该矩形范围的两个角的坐标GLatLng
  function GLatLngBounds(a,b)
  {
   
if(a&&!b)
   {
    b
=a
   }
   
if(a)
   {
    
var c=GetNumberInRange(a.latRadians(),-GMath_PI/2,GMath_PI/2);
    
var d=GetNumberInRange(b.latRadians(),-GMath_PI/2,GMath_PI/2);
    
this.lat=new GLatSegment(c,d);
    
var e=a.lngRadians();
    
var f=b.lngRadians();
    
if(f-e>=GMath_PI*2)
    {
     
this.lng=new GLngSegment(-GMath_PI,GMath_PI)
    }
    
else
    {
     e
=getLoopNumber(e,-GMath_PI,GMath_PI);
     f
=getLoopNumber(f,-GMath_PI,GMath_PI);
     
this.lng=new GLngSegment(e,f)
    }
   }
   
else
   {
    
this.lat=new GLatSegment(1,-1);
    
this.lng=new GLngSegment(GMath_PI,-GMath_PI)
   }
  }
  
//获得中心点坐标的GLatLng
  GLatLngBounds.prototype.getCenter=function()
  {
   
return GLatLng.fromRadians(this.lat.center(),this.lng.center())
  };
  
//转化为字符串
  GLatLngBounds.prototype.toString=function()
  {
   
return"("+this.getSouthWest()+""+this.getNorthEast()+")"
  };
  
//检查是否相等
  GLatLngBounds.prototype.equals=function(a)
  {
   
return this.lat.equals(a.lat)&&this.lng.equals(a.lng)
  };
  
//是否包含指定的GLatLng点
  GLatLngBounds.prototype.contains=function(a)
  {
   
return this.lat.contains(a.latRadians())&&this.lng.contains(a.lngRadians())
  };
  
//返回两个地理区域的重叠地理区域
  GLatLngBounds.prototype.intersects=function(a)
  {
   
return this.lat.intersects(a.lat)&&this.lng.intersects(a.lng)
  };
  
//是否包含指定的GLatLngBounds区域
  GLatLngBounds.prototype.containsBounds=function(a)
  {
   
return this.lat.containSegment(a.lat)&&this.lng.containSegment(a.lng)
  };
  
//扩展本区域以包含指定的GLatLng点
  GLatLngBounds.prototype.extend=function(a)
  {
   
this.lat.extend(a.latRadians());
   
this.lng.extend(a.lngRadians())
  };
  GLatLngBounds.prototype.getSouthWest
=function()
  {
   
return GLatLng.fromRadians(this.lat.min,this.lng.min)
  };
  GLatLngBounds.prototype.getNorthEast
=function()
  {
   
return GLatLng.fromRadians(this.lat.max,this.lng.max)
  };
  
//获得代表该范围大小的GLatLng
  GLatLngBounds.prototype.toSpan=function()
  {
   
return GLatLng.fromRadians(this.lat.span(),this.lng.span(),true)
  };
  
//是否包含所有范围的经度
  GLatLngBounds.prototype.isFullLng=function()
  {
   
return this.lng.isFull()
  };
  
//是否包含所有范围的纬度
  GLatLngBounds.prototype.isFullLat=function()
  {
   
return this.lat.max>=GMath_PI/2&&this.lat.min<=GMath_PI/2
  };
  GLatLngBounds.prototype.isEmpty
=function()
  {
   
return this.lat.isEmpty()||this.lng.isEmpty()
  };
  GLatLngBounds.prototype.qf
=function(a)
  {
   
var b=this.toSpan();
   
var c=a.toSpan();
   
return b.lat()>c.lat()&&b.lng()>c.lng()
  };
  
//本类代表一个经度范围
  function GLngSegment(a,b)
  {
   
if(a==-GMath_PI&&b!=GMath_PI)a=GMath_PI;
   
if(b==-GMath_PI&&a!=GMath_PI)b=GMath_PI;
   
this.min=a;
   
this.max=b
  }
  
//min和max的位置是否相反,如果是,那说明该范围跨越为经度0的那个点
  GLngSegment.prototype.opposite=function()
  {
   
return this.min>this.max
  };
  
//大小是否为空
  GLngSegment.prototype.isEmpty=function()
  {
   
return this.min-this.max==2*GMath_PI
  };
  
//是否包含所有的经度
  GLngSegment.prototype.isFull=function()
  {
   
return this.max-this.min==2*GMath_PI
  };
  
//返回两个GLngSegment的交叉区域
  GLngSegment.prototype.intersects=function(a)
  {
   
var b=this.min;
   
var c=this.max;
   
if(this.isEmpty()||a.isEmpty())return false;
   
if(this.opposite())
   {
    
return a.opposite()||a.min<=this.max||a.max>=b
   }
   
else
   {
    
if(a.opposite())return a.min<=c||a.max>=b;
    
return a.min<=c&&a.max>=b
   }
  };
  
//本范围是否包含制定的经度范围
  GLngSegment.prototype.containSegment=function(a)
  {
   
var b=this.min;
   
var c=this.max;
   
if(this.opposite())
   {
    
if(a.opposite())return a.min>=b&&a.max<=c;
    
return(a.min>=b||a.max<=c)&&!this.isEmpty()
   }
   
else
   {
    
if(a.opposite())return this.isFull()||a.isEmpty();
    
return a.min>=b&&a.max<=c
   }
  };
  
//本范围是否包含制定的经度
  GLngSegment.prototype.contains=function(a)
  {
   
if(a==-GMath_PI)a=GMath_PI;
   
var b=this.min;
   
var c=this.max;
   
if(this.opposite())
   {
    
return(a>=b||a<=c)&&!this.isEmpty()
   }
   
else
   {
    
return a>=b&&a<=c
   }
  };
  
//扩展本范围以包含指定的经度
  GLngSegment.prototype.extend=function(a)
  {
   
if(this.contains(a))return;
   
if(this.isEmpty())
   {
    
this.max=a;
    
this.min=a
   }
   
else
   {
    
if(this.distance(a,this.min)<this.distance(this.max,a))
    {
     
this.min=a
    }
    
else
    {
     
this.max=a
    }
   }
  };
  
//判断两个范围是否相等
  GLngSegment.prototype.equals=function(a)
  {
   
if(this.isEmpty())return a.isEmpty();
   
return getMathAbs(a.min-this.min)%2*GMath_PI+getMathAbs(a.max-this.max)%2*GMath_PI<=1.0E-9
  };
  
//这个其实是一个静态函数,用来返回两个经度的差
  GLngSegment.prototype.distance=function(a,b)
  {
   
var c=b-a;
   
if(c>=0)return c;
   
return b+GMath_PI-(a-GMath_PI)
  };
  
//返回本范围的弧度大小
  GLngSegment.prototype.span=function()
  {
   
if(this.isEmpty())
   {
    
return 0
   }
   
else if(this.opposite())
   {
    
return 2*GMath_PI-(this.min-this.max)
   }
   
else
   {
    
return this.max-this.min
   }
  };
  
//获取本范围的中心经度
  GLngSegment.prototype.center=function()
  {
   
var a=(this.min+this.max)/2;
   
if(this.opposite())
   {
    a
+=GMath_PI;
    a
=getLoopNumber(a,-GMath_PI,GMath_PI)
   }
   
return a
  };
  
//本类代表一个纬度范围
  function GLatSegment(a,b)
  {
   
this.min=a;
   
this.max=b
  }
  GLatSegment.prototype.isEmpty
=function()
  {
   
return this.min>this.max
  };
  
//判断本范围和指定范围有没有相交
  GLatSegment.prototype.intersects=function(a)
  {
   
var b=this.min;
   
var c=this.max;
   
if(b<=a.min)
   {
    
return a.min<=c&&a.min<=a.max
   }
   
else
   {
    
return b<=a.max&&b<=c
   }
  };
  
//判断本范围是否包含制定的纬度范围
  GLatSegment.prototype.containSegment=function(a)
  {
   
if(a.isEmpty())return true;
   
return a.min>=this.min&&a.max<=this.max
  };
  
//判断本范围是否包含指定的纬度
  GLatSegment.prototype.contains=function(a)
  {
   
return a>=this.min&&a<=this.max
  };
  
//扩展本范围以包含指定的纬度
  GLatSegment.prototype.extend=function(a)
  {
   
if(this.isEmpty())
   {
    
this.min=a;
    
this.max=a
   }
   
else if(a<this.min)
   {
    
this.min=a
   }
   
else if(a>this.max)
   {
    
this.max=a
   }
  };
  
//判断2个纬度范围是否相同
  GLatSegment.prototype.equals=function(a)
  {
   
if(this.isEmpty())return a.isEmpty();
   
return getMathAbs(a.min-this.min)+getMathAbs(this.max-a.max)<=1.0E-9
  };
  
//返回纬度范围的弧度大小
  GLatSegment.prototype.span=function()
  {
   
return this.isEmpty()?0:this.max-this.min
  };
  
//返回纬度范围的中点
  GLatSegment.prototype.center=function()
  {
   
return(this.max+this.min)/2
  };

        以上四个类(其实只有2个因为GLngSegment和GLatSegment主要是提供对GLatLngBounds的支持,没有开放)以前是在GPoint和GBounds之中使用的,这次划分出来之后,主要是添加了一些弧度转换和范围验证,从这些方面来看,分开还是有一些必要的,不过似乎也没有太大的不同,尤其是对用户来讲。

        到现在我的网站都没有开始从1.0向2.0的升级,不过想起来,应该是非常费劲的,不过不久之后我就要开始这个工作了。

        另外,对文档的学习和翻译也在进行之中

posted on 2006-05-11 19:38  K_Reverter  阅读(4155)  评论(2编辑  收藏  举报