我正在尝试使用下拉列表更改google-map中的半径圆,例如当我从下拉列表中选择2 km时,它将被设置为2.0半径,如果我选择4 km则将设置为4.0到半径。在这一点上,一切都工作得很完美,但现在问题是在第二次选择第一个存在时,没有从圆圈中删除。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title></title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.js'></script>
<link rel="stylesheet" type="text/css" href="/css/normalize.css">
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<style type='text/css'>
#map_canvas {
background:#ccc;
height:400px;
width:50%;
margin-top:15px;
}
</style>
<select name="distance" id="distance">
<option value="0.5" selected="selected">0.5 km</option>
<option value="1.0">1 km</option>
<option value="2.0">2 km</option>
<option value="3.0">3 km</option>
<option value="4.0">4 km</option>
<option value="5.0">5 km</option>
</select>
<script type='text/javascript'>//<![CDATA[
jQuery.noConflict();
jQuery(document).ready(function() {
var subjectRange;
var myOptions = {
zoom: 13,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var locations = ['1.280095,103.850949'];
/**
* Draw points and check whether those points are inside a range from a point.
*/
var subjectPoint = {
point: new google.maps.LatLng(1.280095,103.850949),
radius: 1.0, //default radius
color: '#00AA00',
}
var elevator;
var map;
var geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(1.280095,103.850949);
//render the range
var subjectMarker = new google.maps.Marker({
position: subjectPoint.point,
title: 'Subject',
});
var subjectRange = new google.maps.Circle({
map: map,
radius: subjectPoint.radius * 1000, // metres
fillColor: subjectPoint.color,
strokeColor: '#3D9912'
});
subjectRange.bindTo('center', subjectMarker, 'position');
function codeAddress(locations) {
for (i = 0; i < locations.length; i++) {
geocoder.geocode( { 'address': locations[i]}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
}
codeAddress(locations);
jQuery('#distance').on('change', function() {
alert(jQuery('select[name="distance"] option:selected').val());
rad = jQuery('select[name="distance"] option:selected').val();
//subjectRange.setMap(null);
var subjectPoint = {
point: new google.maps.LatLng(1.280095,103.850949),
radius: rad,
color: '#00AA00',
}
//render the range
var subjectMarker = new google.maps.Marker({
position: subjectPoint.point,
title: 'Subject',
});
var subjectRange = new google.maps.Circle({
map: map,
radius: subjectPoint.radius * 1000, // metres
fillColor: subjectPoint.color,
strokeColor: '#3D9912'
});
subjectRange.bindTo('center', subjectMarker, 'position');
});
});//]]>
</script>
</head>
<body>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<div id="map_canvas">Google Map</div>
</body>
</html>
答案 0 :(得分:4)
每次更改半径时,您都会创建一个新圆圈。只需更改现有圆的半径:
jQuery('#distance').on('change', function() {
rad = jQuery('select[name="distance"] option:selected').val();
//render the range
subjectRange.setOptions({radius:rad * 1000});
});