我认为这个问题与 Google Maps v2 有关,因为我认为 v3 中没有类型:G_SATELLITE_3D_MAP。
通过google-maps-utility-library-v3 (googleearth.js) 中的脚本支持用于 Google Maps v3 的 Google 地球插件(测试版)
作者@jlivni采取的方法是监听Google Maps v3 添加/删除事件并添加相应的Google Earth Api 对象。我相信类似的方法可以用于 OpenLayers。我是 OpenLayers 的新手,刚刚开始研究这个,但我会在这里发布更新。
我现在唯一可以添加的是关于谷歌地球插件的初始化,这在 v3 中有所不同:
function GoogleEarth( olmap ) {
this.olmap_ = olmap;
this.layer_ = new OpenLayers.Layer.Google(
"Google Earth",
{type: google.maps.MapTypeId.SATELLITE, numZoomLevels: 22, visibility: false}
);
this.olmap_.addLayers([ this.layer_ ]);
this.map_ = this.layer_.mapObject;
this.mapDiv_ = this.map_.getDiv();
...
var earthMapType = /** @type {google.maps.MapType} */({
tileSize: new google.maps.Size(256, 256),
maxZoom: 19,
name: this.earthTitle_,
// The alt helps the findMapTypeControlDiv work.
alt: this.earthTitle_,
getTile:
/**
* @param {google.maps.Point} tileCoord the tile coordinate.
* @param {number} zoom the zoom level.
* @param {Node} ownerDocument n/a.
* @return {Node} the overlay.
*/
function(tileCoord, zoom, ownerDocument) {
var div = ownerDocument.createElement('DIV');
return div;
}
});
this.map_.mapTypes.set(GoogleEarth.MAP_TYPE_ID, earthMapType);
this.layer_.type = GoogleEarth.MAP_TYPE_ID;
var that = this;
google.maps.event.addListener(map, 'maptypeid_changed', function() {
that.mapTypeChanged_();
});
}
令人费解的部分是我们需要为 Google Earth 定义一个新的地图类型,并将该类型添加到 Google Map 对象中。但是,如果我们不创建具有我最初设置为 google.maps.MapTypeId.SATELLITE 的类型的图层,则 Google 地图对象不存在。不是很干净,但至少它让我使用 Google Maps v3 达到与本文作者相同的状态。最后可能有一种方法可以通过修改函数 findMapTypeControlDiv_() 使 OpenLayers 控件可见:
var mapTypeControlDiv = this.findMapTypeControlDiv_();
if (mapTypeControlDiv) {
this.setZIndexes_(mapTypeControlDiv);
this.addShim_(mapTypeControlDiv);
}
[更新]
我已经修改了函数 findMapTypeControlDiv_() ,现在我寻找 OpenLayers LayerSwitcher :
GoogleEarth.prototype.findLayerSwitcherDiv_ = function() {
// var title = 'title="' + this.earthTitle_ + '"';
var id = 'LayerSwitcher';
var siblings = this.controlDiv_.parentNode.childNodes;
for (var i = 0, sibling; sibling = siblings[i]; i++) {
if (sibling.id.indexOf(id) != -1) {
return sibling;
}
}
};
LayerSwitcher 的 z 索引设置正确,div 显示在顶部,直到调用 google.earth.createInstance(),然后它消失。我会花更多的时间来解决它,它看起来并不难解决。
[更新 2]
我已经解决了 LayerSwitcher 面板问题。诀窍是将 Google Earth Plugin div 附加到正确的 div(GMaps 的原始代码将其附加到 Map Controls 数组)。另一个问题是设置 zIndex 以确保 LayerSwitcher 位于顶部。当 GE 插件运行时我仍然遇到问题并且我尝试最小化 LayerSwitcher,对 OpenLayers.Event.stop() 的调用会使 GE 插件崩溃(Chrome)或 LayerSwitcher 消失(IE8)。我想从谷歌分叉原始代码并制作一个适用于 OpenLayers 的 GE 插件层。有人可以建议怎么做吗?谢谢。
无论如何,这是我的最新更改:
GoogleEarth.prototype.findLayerSwitcherDiv_ = function() {
var id = 'LayerSwitcher';
var siblings = this.mapDiv_.parentNode.childNodes;
for (var i = 0, sibling; sibling = siblings[i]; i++) {
if (sibling.id.indexOf(id) != -1) {
return sibling;
}
}
};
GoogleEarth.prototype.addEarthControl_ = function() {
...
inner.appendChild(earthDiv);
var parentNode = this.findLayerSwitcherDiv_().parentNode;
parentNode.appendChild( control );
...
GoogleEarth.prototype.setZIndexes_ = function(mapTypeControlDiv) {
var oldIndex = mapTypeControlDiv.style.zIndex;
var siblings = this.controlDiv_.parentNode.childNodes;
for (var i = 0, sibling; sibling = siblings[i]; i++) {
sibling['__gme_ozi'] = sibling.style.zIndex;
// Sets the zIndex of all controls to be behind Earth.
sibling.style.zIndex = 0;
}
mapTypeControlDiv['__gme_ozi'] = oldIndex;
this.controlDiv_.style.zIndex = 2000;
mapTypeControlDiv.style.zIndex = 2001;
};
[更新 3] 我在这里建立了一个 github 项目:https ://github.com/ZiglioUK/GoogleEarth-for-OpenLayers