我重做了这段代码,所以它更短,但基本上我试图用左键单击添加一个圆圈并用右键单击将其删除。radius.setMap(null) 仅在它不在函数内时才有效。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100%% }
body { height: 100%%; margin: 0; padding: 0 }
#map_canvas { height: 100%% }
</style>
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?key=mykey&sensor=false">
</script>
<script type="text/javascript">
var centerlatlng = new google.maps.LatLng(-27.467726,153.026633);
function initialize() {
var mapOptions = {
center: centerlatlng,
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
var marker = new google.maps.Marker({
position: centerlatlng,
map: map,
title:"Hello World!"});
google.maps.event.addDomListener(map, 'click', updateCircle);
google.maps.event.addDomListener(map, 'rightclick', removeRadius);
function removeRadius(){
radius.setMap(null);}
function updateCircle(){
var radius = new google.maps.Circle({map: map,
radius: 400,
center: centerlatlng,
fillOpacity: 0});}
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width:50%%; height:50%%"></div>
<body>
</html>