我有一个 XML,它通过 XSLT 转换为 HTML。 XML 能够包含 JavaScript,并将其正确地转换为 HTML,正如我在许多其他页面中所做的那样。它只是不适用于 GoogleMaps,我怀疑我的 JavaScript 哪里有问题。
生成的 HTML 的相关部分如下所示。
HTML/脚本中发生了什么:
<body
onload="start();"> 启动.google.maps完全没有。对我来说,这看起来像是以下某处有问题。
var map_options = {
center: new google.maps.LatLng(44.54, -78.54),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
但几个小时后我仍然无法弄清楚它是什么。
<html>
<head>
...
<script src="http://maps.googleapis.com/maps/api/js?sensor=false" type="text/javascript">
</script>
<script type="text/javascript">
function start()
{
var map_canvas = document.getElementById('map_canvas');
// If the element can be located, we make it green. Checked.
// If we give the function another name than start, it won't be executed. Checked.
map_canvas.style.backgroundColor = 'green';
if(typeof google.maps != 'undefined') {
// If the google.maps object exists, we make the canvas red. Checked.
// If the API was not loaded from googleapis (commented out), it will stay green.
map_canvas.style.backgroundColor = 'red';
}
// So why does the following not work?
var map_options = {
center: new google.maps.LatLng(44.54, -78.54),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
// Not arriving here:
map_canvas.style.backgroundColor = 'blue';
// Before going on, I want to solve above problem, so this is commented out.
// var map = new google.maps.Map(map_canvas, map_options);
// map_canvas.style.backgroundColor = 'black';
}
</script>
</head>
<body onload="start();">
...
<div style="width: 400px; height: 224px;" id="map_canvas"></div>
...
</body>
</html>
最佳答案
我发现导致此错误的最常见原因仅仅是无效的 img src。
关于javascript - 无效状态错误 : "An attempt was made to use an object that is not, or is no longer, usable" in basic Google Map tutorial example,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17212508/