我已按照 Google Maps API v3 教程创建基本地图。我试图弄清楚如何最初声明地图,然后允许我根据人们选择的“类别”添加点。我还试图弄清楚如何在每个选择之间清除地图。
问问题
248 次
1 回答
0
有一个类别的表单字段。更改它会触发一个 JS 函数,该函数会清除地图中的所有标记,然后为该类别添加一些标记。如果您使用到目前为止尝试过的代码来扩展您的问题,我可以扩展此答案。
要清除标记,marker.setMap(null);
请在每个标记上使用(没有 API 函数可以为您清除所有标记)。
您可能还需要一个 JS 数组或类别的关联数组,其中包含每个标记的数据(至少是 lat/lng 值),这将使您的函数变得容易。
好的,这是一个可行的解决方案(为简洁起见,我减少了标记等的数量)。
<!DOCTYPE html>
<html>
<head>
<title>http://stackoverflow.com/questions/15550211/populate-clear-google-map/15550488?noredirect=1#comment22037226_15550488</title>
<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 { width:600px; height:500px }
</style>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
var MSU;
var map;
var BusRouteButton = document.getElementById('Add-Bus-Route');
var arrRoutes = [
{
name : "Mustang",
color : "#660000",
route : [
new google.maps.LatLng(33.87528, -98.51848),
new google.maps.LatLng(33.87045, -98.51856),
new google.maps.LatLng(33.86926, -98.51844),
new google.maps.LatLng(33.86666, -98.51833),
new google.maps.LatLng(33.86503, -98.51849),
new google.maps.LatLng(33.87529, -98.52366),
new google.maps.LatLng(33.87524, -98.52195),
new google.maps.LatLng(33.87521, -98.52026),
new google.maps.LatLng(33.87528, -98.51848)
],
stops : [
[33.87262, -98.51856],
[33.86503, -98.51849],
[33.87529, -98.52366]
],
markers : []
},
{
name : "Foobar",
color : "#000066",
route : [
new google.maps.LatLng(33.87528, -98.51848),
new google.maps.LatLng(33.87524, -98.52195),
new google.maps.LatLng(33.86926, -98.51844),
new google.maps.LatLng(33.87184, -98.52376)
],
stops : [
[33.87528, -98.51848],
[33.87184, -98.52376]
],
markers : []
}
];
function initialize() {
MSU = new google.maps.LatLng(33.87356, -98.52130);
var mapOptions = {
center: MSU,
zoom: 16,
zoomControl: true,
mapTypeControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
// lets add the data from the array to the page! I'm going to use jQuery's append for this, you could also use document.write if you aren't using it
for (var i = 0; i < arrRoutes.length; i++) {
$('#category').append(
'<option value="' + i + '">' + arrRoutes[i].name + '</option>'
);
}
}
function BusRouteMarkers() {
// what's the currently selected category?
var intRoute = $('#category').val();
for (var i = 0; i < arrRoutes.length; i++) {
// better get rid of any previous polylines first
if (arrRoutes[i].polyline) {
arrRoutes[i].polyline.setMap(null);
}
// ditto for any previous markers
for (var j = 0; j < arrRoutes[i].markers.length; j++) {
arrRoutes[i].markers[j].setMap(null);
}
}
// add this polyline to the array for reference later
arrRoutes[intRoute].polyline = new google.maps.Polyline({
path: arrRoutes[intRoute].route,
strokeColor: arrRoutes[intRoute].color,
strokeOpacity: 1.0,
strokeWeight: 4,
map: map
});
for (var i = 0; i < arrRoutes[intRoute].stops.length; i++) {
arrRoutes[intRoute].markers.push(
new google.maps.Marker({
position: new google.maps.LatLng(
arrRoutes[intRoute].stops[i][0],
arrRoutes[intRoute].stops[i][1]
),
map: map
})
);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"></div>
<button id="Add-Bus-Route" onClick="BusRouteMarkers()">click me</button>
<select id="category">
</select>
</body>
</html>
于 2013-03-21T14:36:39.997 回答