我有通常的google maps api启动代码:
function initialize() {
var mapOptions = {
zoom: 7,
zoomControl : false,
streetViewControl : false,
panControl : false,
scaleControl : true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
};
</script>
我还有一组定义叠加层的文件,如下所示:
var path12coords = [
//stuff
];
var path12 = new google.maps.Polyline({
path: path12coords,
strokeOpacity: 1.0,
strokeWeight: 2
});
path12.setMap(map);
此代码必须进入initialize()
方法,或者在加载时以任何其他方式调用。我如何将其添加到我的代码中?我可以<script type="text/javascript" src="js/path12.js"></script>
,但代码是独立的,不会在加载时调用。
答案 0 :(得分:0)
将其添加到初始化函数。如果您的代码变得很大,您可以定义自己的函数并在初始化中调用这些函数。
如果您想使用外部js文件,则取决于您是否要在其他页面上重复使用该代码。这取决于代码的大小。
答案 1 :(得分:0)
在initialize
内,您可以添加:
var s=document.createElement('script');
s.src='js/path12.js';
document.getElementsByTagName('head')[0].appendChild(s);