我一直在将 addPoly 函数的内容用于初始化函数,并且效果很好。但是,如果我创建一个函数 addPoly 并从初始化函数中调用它,它将不起作用。我想让用户选择一个形状。因此,单击按钮我可以调用特定的形状函数。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<style type="text/css">
html,body {height:100%;}
#map-canvas {height:92%;width:100%;}
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">
var shape;
function initialize() {
var mapDiv = document.getElementById('map-canvas');
var map = new google.maps.Map(mapDiv, {
center: new google.maps.LatLng(24.886436490787712, -70.2685546875),
zoom: 4,
mapTypeId: google.maps.MapTypeId.ROADMAP,
zoomControl: true
});
addPoly();
}
function addPoly(){
shape = new google.maps.Polygon({
strokeColor: '#ff0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#ff0000',
fillOpacity: 0.35
});
shape.setMap(map);
google.maps.event.addListener(map, 'click', addPoint);
}
function clearAll(){
shape.setMap(null);
}
function addPoint(e) {
var vertices = shape.getPath();
vertices.push(e.latLng);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body style="font-family: Arial; border: 0 none;">
<div id="map-canvas"></div>
<input type="button" value="Clear" onclick="clearAll()">
Selection Type:
<select id="selectType">
<option type="button" value="Polygon" onclick="initialize()">Polygon</option>
<option type="button" value="Circle" onclick="initialize()">Circle</option>
</select>
</body>
</html>
</p>