我有一个带有地图的网络应用。我添加了一个漂亮的小自定义地图控件来打开和关闭地图上的不同图层。目前只有两层,在大多数浏览器中它都很好用。
IE8 + 7除外。打开时,地图上没有显示任何图层。据我所知,地图正在加载kmz / kml文件(当preserveViewport设置为false时,地图移动到正确的位置)但它们只是没有出现。一个图层包含折线,另一个图层包含标记。我使用的代码如下:
function someFunction() {
//code to initialise map etc goes here...
var layers = [];
//Create 1st layer
var exchangeslayer = new google.maps.KmlLayer('http://link.to.file/exchanges.kmz'
suppressInfoWindows: true,
preserveViewport: true
});
layers.push({name: "Exchanges", layer: exchangeslayer});
//Code to create second layer
var nyclayer = new google.maps.KmlLayer('http://www.nyc.gov/html/dot/downloads/misc/cityracks.kml'
suppressInfoWindows: true,
preserveViewport: false
});
layers.push({name: "NY City Tracks", layer: nyclayer});
addCustomLayerControls(layers);
}
function addCustomLayerControls(layers) {
//there is code here that would generate the divs for the custom map control
var container; //container is a div element created via javascript
for (var i = 0; i < layers.length; i++) {
this.addLayerLabelToContainer(layers[i], container);
}
//some more code
}
function addLayerLabelToContainer(layer, container) {
var map; //Assume I get a reference to the map
//some code here to make pretty labels for the map controls...
var layerLabel; // layerLabel is a div element created via javascript
google.maps.event.addDomListener(layerLabel, 'click', function() {
if(layer.layer.map == null) {
layer.layer.setMap(map);
} else {
layer.layer.setMap(null);
}
});
}
答案 0 :(得分:2)
因为事实证明我的问题与CSS有关。我的一个样式表将max-width: 100%
应用于所有img标记。这是用地图标记/折线打哈佛。
现在很明显我看到了它,但是当你认为问题与javascript有关时就不那么明显了。因此,我会把这个答案留给任何与我犯同样错误的人。
答案 1 :(得分:0)
如果您像这样修改 addLayerLabelToContainer (),那么它可以按预期在IE中运行。已验证它在IE 8和9中正确加载KMZ。
function addLayerLabelToContainer(layer, container) {
// var map; //Assume I get a reference to the map
//some code here to make pretty labels for the map controls...
var layerLabel; // layerLabel is a div element created via javascript
if(layer.layer.map == null) {
layer.layer.setMap(map);
} else {
layer.layer.setMap(null);
}
}
不需要调用addDomListener()。另请注意API syntax:
addDomListener(instance:Object, eventName:string, handler:Function)
someFunction 中语法错误的小修复如下:
function someFunction() {
// var map; //assume map is initialised, I've just removed that code
var layers = [];
// see https://developers.google.com/maps/documentation/javascript/layers
//Create 1st layer
var exchangeslayer = new google.maps.KmlLayer(
'http://kml-samples.googlecode.com/svn/trunk/kml/kmz/simple/big.kmz',
{ suppressInfoWindows: true, preserveViewport: true
});
layers.push( {name: "Exchanges", layer: exchangeslayer} );
// ...
addCustomLayerControls(layers);
}