[英]Google Maps toggle layer
我創建了一個按鈕來切換雷達圖層。 默認情況下,加載頁面時,圖層處於關閉狀態。 這就是我想要的並且完美的作品。 我按下“雷達”按鈕,雷達覆蓋層打開。 該部分也很棒。 我的問題是當我再次將其關閉以將其關閉時,它只關閉一秒鍾,然后又重新打開。 我想念什么?
var radarOptions = {
gmap: map,
name: 'Radar',
position: google.maps.ControlPosition.TOP_RIGHT,
action: function(){
map.overlayMapTypes.push(null); // create empty overlay entry
map.overlayMapTypes.setAt("1",tileNEX);
}
}
var radarButton = new buttonControl(radarOptions);
編輯:
這是我同時用於按鈕和圖層的當前更新代碼。 它會亮起,但是當我將其關閉時,它只熄滅一秒鍾,然后又重新亮起。
/
/set up custom buttons
var radarOptions = {
gmap: map,
name: 'Radar',
position: google.maps.ControlPosition.TOP_RIGHT,
action: function(){
map.overlayMapTypes.push(null); // create empty overlay entry
map.overlayMapTypes.setAt("1",tileNEX);
}
}
var radarButton = new buttonControl(radarOptions);
tileNEX = new google.maps.ImageMapType({
getTileUrl: function(tile, zoom) {
return "http://mesonet.agron.iastate.edu/cache/tile.py/1.0.0/nexrad-n0q-900913/" + zoom + "/" + tile.x + "/" + tile.y +".png?"+ (new Date()).getTime();
},
tileSize: new google.maps.Size(256, 256),
opacity:0.70,
name : 'NEXRAD',
isPng: true
});
您只需要清除它:
map.overlayMapTypes.clear();
或者,您可以彈出最新的:
map.overlayMapTypes.pop();
或者,如果您有多個圖層,請獲取所需圖層的索引,然后執行以下操作:
map.overlayMapTypes.removeAt(index);
編輯:
您可能需要將操作替換為以下內容:
action: function(){
if (map.overlayMapTypes.length==0) {
map.overlayMapTypes.push(null); // create empty overlay entry
map.overlayMapTypes.setAt("1",tileNEX);
}
else {
map.overlayMapTypes.clear();
}
}
我知道頁面加載時,如果您打開控制台並運行map.overlayMapTypes.length
它將輸出0; 一旦您擊中雷達,它就會輸出2(不確定為什么不是1,但無論如何)。 因此,我們要做的就是檢查它是否具有一層,如果沒有,我們將執行您的常規代碼(因為它應該將其打開)。 如果已經有一個,我們清除它們。 您的代碼可能有所不同,我假設此操作是您處理所有點擊的地方,否則,您可能需要嘗試一下。
上一次編輯:如果出現錯誤消息“ about missing}”,請找出放置在}中的位置。
var radarOptions = {
gmap: map,
name: 'Radar',
position: google.maps.ControlPosition.TOP_RIGHT,
action: function(){
if (map.overlayMapTypes.length==0) {
map.overlayMapTypes.push(null); // create empty overlay entry
map.overlayMapTypes.setAt("1",tileNEX);
}
else {
map.overlayMapTypes.clear();
}
}
}
var radarButton = new buttonControl(radarOptions);
tileNEX = new google.maps.ImageMapType({
getTileUrl: function(tile, zoom) {
return "http://mesonet.agron.iastate.edu/cache/tile.py/1.0.0/nexrad-n0q-900913/" + zoom + "/" + tile.x + "/" + tile.y +".png?"+ (new Date()).getTime();
},
tileSize: new google.maps.Size(256, 256),
opacity:0.70,
name : 'NEXRAD',
isPng: true
});
如果要在索引處刪除,請在設置它的位置刪除它:
map.overlayMapTypes.setAt(1,tileNEX);
要將其從索引中刪除,請使用以下命令:
map.overlayMapTypes.removeAt(1, tileNex);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.