如何在google maps api v3中调整TMS图层的不透明度?

时间:2014-07-17 04:44:34

标签: javascript google-maps-api-3

我在谷歌地图中有以下TMS图层:

KantoRapidMapType.prototype.tileSize = new google.maps.Size(256,256);
KantoRapidMapType.prototype.maxZoom = 17;
KantoRapidMapType.prototype.name = "Rapid Survey";
KantoRapidMapType.prototype.alt = "Rapid Survey Map in Kanto Plain";
KantoRapidMapType.prototype.getTile = function(tile, zoom, ownerDocument) {
  var img = ownerDocument.createElement("img");
  img.style.width = this.tileSize.width + "px";
  img.style.height = this.tileSize.height + "px";
  var y = (1<<zoom) - tile.y - 1;
  img.src = "http://www.finds.jp/ws/tms/"
    +"1.0.0/"
    +"Kanto_Rapid-900913/"
    + zoom + "/" + tile.x + "/" + y + ".png";
  return img;
};

我添加了TMS图层:     var kanto = new KantoRapidMapType();     map.overlayMapTypes.insertAt(0,kanto);

如何设置此图层的不透明度?当我尝试以下任何事情时:

kanto.setOpacity(0.5)

我只是未定义,因为kanto没有setOpacity函数。我注意到ImageMapType似乎支持setOpacity,但我不确定是否可以包装或转换我所拥有的内容。

1 个答案:

答案 0 :(得分:0)

实施方法getTileUrl而不是getTile

  KantoRapidMapType=function(opts){
    //apply supplied options
    opts=opts||{};
    for(var k in opts){
      this[k]=opts[k];
    }
  }
  KantoRapidMapType.prototype.tileSize = new google.maps.Size(256,256);
  KantoRapidMapType.prototype.maxZoom = 17;
  KantoRapidMapType.prototype.name = "Rapid Survey";
  KantoRapidMapType.prototype.alt = "Rapid Survey Map in Kanto Plain";
  KantoRapidMapType.prototype.getTileUrl=function(tile,zoom){
    var y = (1<<zoom) - tile.y - 1;
      return("http://www.finds.jp/ws/tms/"
              +"1.0.0/"
              +"Kanto_Rapid-900913/"
              + zoom + "/" + tile.x + "/" + y + ".png");
  }

继承ImageMapType的方法创建一个新的ImageMapType实例,并将一个新的KantoRapidMapType实例作为参数传递:

var kanto = new google.maps.ImageMapType(new KantoRapidMapType());
//set the opacity
kanto.setOpacity(.5);

或使用选项直接传递不透明度:

var kanto = new google.maps.ImageMapType(new KantoRapidMapType({opacity:.5}));