K_Reverter的网页开发记录

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

导航

Google Maps API 2.0解析(10-GMapType GTileLayer _GoogleMapMercSpec _KeyholeMapMercSpec地图类型)

 

//地图类型类,构造函数参数分别为:
  // a 地图层GTileLayer数组
  // b 使用的坐标系统
  // c 名称
  // d 配置类
  function GMapType(a,b,c,d)
  {
   
var e=d||{};
   
var f=this;
   f.layers
=a||[];
   f.name
=c||"";
   f.projection
=b||new GProjection();//坐标系统
   f.shortName=e.shortName||c;
   f.urlArg
=e.urlArg||"c";
   f.maxResolution
=e.maxResolution||getComparedMethodResult(a,GTileLayer.prototype.maxResolution,Math.max);
   f.minResolution
=e.minResolution||getComparedMethodResult(a,GTileLayer.prototype.minResolution,Math.min);
   f.textColor
=e.textColor||"black";
   f.linkColor
=e.linkColor||"#7777cc";
   f.errorMessage
=e.errorMessage||"";
   f.tileSize
=e.tileSize||256;
   
for(var g=0;g<a.length;++g)
   {
    bind(a[g],GEvent_newcopyright,f,f.onNewCopyRight)
   }
  }
  GMapType.prototype.getName
=function(a)
  {
   
return a?this.shortName:this.name
  };
  
//返回坐标系统
  GMapType.prototype.getProjection=function()
  {
   
return this.projection
  };
  GMapType.prototype.getTileLayers
=function()
  {
   
return this.layers
  };
  
//返回所有的GCopyRight
  GMapType.prototype.getCopyright=function(a,b)
  {
   
var c=this.layers;
   
var d=[];
   
for(var e=0;e<c.length;e++)
   {
    
var f=c[e].getCopyright(a,b);
    
if(f)
    {
     d.push(f)
    }
   }
   
if(d.length>0)
   {
    
return d.join("")
   }
   
return null
  };
  GMapType.prototype.getMinimumResolution
=function(a)
  {
   
return this.minResolution
  };
  GMapType.prototype.getMaximumResolution
=function(a)
  {
   
return this.maxResolution
  };
  GMapType.prototype.getTextColor
=function()
  {
   
return this.textColor
  };
  GMapType.prototype.getLinkColor
=function()
  {
   
return this.linkColor
  };
  GMapType.prototype.getErrorMessage
=function()
  {
   
return this.errorMessage
  };
  GMapType.prototype.getUrlArg
=function()
  {
   
return this.urlArg
  };
  GMapType.prototype.getTileSize
=function()
  {
   
return this.tileSize
  };
  
//根据经纬度中心和到中心的距离来返回最合适的缩放等级
  GMapType.prototype.getSpanZoomLevel=function(a,b,c)
  {
   
var d=this.projection;
   
var e=this.maxResolution;
   
var f=this.minResolution;
   
var g=getMathRound(c.width/2);
   
var h=getMathRound(c.height/2);
   
for(var i=e;i>=f;--i)
   {
    
var l=d.fromLatLngToPixel(a,i);
    
var n=new GPoint(l.x-g-3,l.y+h+3);
    
var p=new GPoint(n.x+c.width+3,n.y-c.height-3);
    
var t=new GLatLngBounds(d.fromPixelToLatLng(n,i),d.fromPixelToLatLng(p,i));
    
var v=t.toSpan();
    
if(v.lat()>=b.lat()&&v.lng()>=b.lng())
    {
     
return i
    }
   }
   
return 0
  };
  
//根据经纬度范围和显示区域大小来返回最合适的缩放等级
  GMapType.prototype.getBoundsZoomLevel=function(a,b)
  {
   
var c=this.projection;
   
var d=this.maxResolution;
   
var e=this.minResolution;
   
var f=a.getSouthWest();
   
var g=a.getNorthEast();
   
for(var h=d;h>=e;--h)
   {
    
var i=c.fromLatLngToPixel(f,h);
    
var l=c.fromLatLngToPixel(g,h);
    
if(i.x>l.x)
    {
     i.x
-=c.getWrapWidth(h)
    }
    
if(getMathAbs(l.x-i.x)<=b.width&&getMathAbs(l.y-i.y)<=b.height)
    {
     
return h
    }
   }
   
return 0
  };
  GMapType.prototype.onNewCopyRight
=function()
  {
   trigger(
this,GEvent_newcopyright)
  };
  
//抽象类GTileLayer,用来通过继承本GTileLayer生成自己的地图层
  function GTileLayer(a,b,c)
  {
   
this.copyrights=a||new GCopyrightCollection();
   
this.minResolution=b||0;
   
this.maxResolution=c||0;
   bind(a,GEvent_newcopyright,
this,this.onNewCopyRight)
  }
  GTileLayer.prototype.minResolution
=function()
  {
   
return this.minResolution
  };
  GTileLayer.prototype.maxResolution
=function()
  {
   
return this.maxResolution
  };
  GTileLayer.prototype.getTileUrl
=function(a,b)
  {
   
return GPath_transImg
  };
  GTileLayer.prototype.isPng
=function()
  {
   
return false
  };
  GTileLayer.prototype.getOpacity
=function()
  {
   
return 1
  };
  GTileLayer.prototype.getCopyright
=function(a,b)
  {
   
return this.copyrights.getCopyrightsString(a,b)
  };
  GTileLayer.prototype.onNewCopyRight
=function()
  {
   trigger(
this,GEvent_newcopyright)
  };
  
//这是Google地图模式的类,构造函数分别为:
  // a 服务器列表
  // b 版权信息名称
  // c 缩放等级个数
  // d 是否使用PNG格式的图片
  function _GoogleMapMercSpec(a,b,c,d)
  {
   GTileLayer.call(
this,b,0,c);//运行基类的构造函数
   this.serviceUrls=a;
   
this.usePng=d||false
  }
  InheritClass(_GoogleMapMercSpec,GTileLayer); 
//继承基类的方法
  _GoogleMapMercSpec.prototype.getTileUrl=function(a,b) //覆盖getTileUrl方法
  {
   b
=this.maxResolution()-b;
   
var c=(a.x+a.y)%this.serviceUrls.length;//循环使用服务器的地址
   return this.serviceUrls[c]+"x="+a.x+"&y="+a.y+"&zoom="+b
  };
  _GoogleMapMercSpec.prototype.isPng
=function()
  {
   
return this.usePng
  };
  
//这是卫星图模式的类,构造函数分别为:
  // a 服务器列表
  // b 版权信息名称
  // c 缩放等级个数
  // d 
  function _KeyholeMapMercSpec(a,b,c,d,e)
  {
   GTileLayer.call(
this,b,0,c);//运行基类的构造函数
   this.serviceUrls=a;
   
if(d) //将_mSatelliteToken存放在Cookie之中或者存在网址之中以发送给服务器
   {
    
this.Dg(d,e)
   }
  }
  InheritClass(_KeyholeMapMercSpec,GTileLayer); 
//继承基类的方法
  _KeyholeMapMercSpec.prototype.Dg=function(a,b)
  {
   
if(testCookie(b))
   {
    document.cookie
="khcookie="+a+"; domain=."+b+"; path=/kh;"
   }
   
else
   {
    
for(var c=0;c<this.serviceUrls.length;++c)
    {
     
this.serviceUrls[c]+="cookie="+a+"&"
    }
   }
  };
  
//检查Cookie是否可用
  function testCookie(a)
  {
   
try
   {
    document.cookie
="testcookie=1; domain=."+a;
    
if(document.cookie.indexOf("testcookie")!=-1)
    {
     document.cookie
="testcookie=; domain=."+a+"; expires=Thu, 01-Jan-70 00:00:01 GMT";
     
return true
    }
   }
   
catch(b)
   {
   }
   
return false
  }
  _KeyholeMapMercSpec.prototype.getTileUrl
=function(a,b) //覆盖getTileUrl方法,这个方法也就是卫星图的文件名加密算法
  {
   
var c=Math.pow(2,b);
   
var d=a.x;
   
var e=a.y;
   
var f="t";
   
for(var g=0;g<b;g++)
   {
    c
=c/2;
    
if(e<c)
    {
     
if(d<c)
     {
      f
+="q"
     }
     
else
     {
      f
+="r";
      d
-=c
     }
    }
    
else
    {
     
if(d<c)
     {
      f
+="t";
      e
-=c
     }
     
else
     {
      f
+="s";
      d
-=c;
      e
-=c
     }
    }
   }
   
var h=(a.x+a.y)%this.serviceUrls.length;
   
return this.serviceUrls[h]+"t="+f
  };

        这几个类是非常重要的,因为根据经纬度算出图片路径的方法就在其中,而且,假如你要创建自己的地图类型,你可能也要仔细研究这段代码,通过实现GTileLayer方法来达到自己的目的。

posted on 2006-05-14 12:29  K_Reverter  阅读(2627)  评论(4编辑  收藏  举报