我需要在选项更改时清除/删除所有先前的标记。我尝试在 Maps API 文档和 Stackoverflow 中搜索答案,不幸的是我不是 JavaScript 专家。它们似乎都以不同的方式实施,而我并没有成功地让它发挥作用。我最接近的,是删除一个标记。
我感谢提供的任何帮助,谢谢!
map = new google.maps.Map(document.getElementById('map_canvas'), {
zoom: 14,
center: new google.maps.LatLng(40.680099,-73.945693),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
infowindow = new google.maps.InfoWindow();
var marker, i;
var markerShadow = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_shadow",
new google.maps.Size(40, 37),
new google.maps.Point(0, 0),
new google.maps.Point(12, 35));
var restaurantColor = "0080FF";
var restaurantMarker = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|" + restaurantColor,
new google.maps.Size(21, 34),
new google.maps.Point(0,0),
new google.maps.Point(10, 34));
var takeoutColor = "79CEFF";
var takeoutMarker = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|" + takeoutColor,
new google.maps.Size(21, 34),
new google.maps.Point(0,0),
new google.maps.Point(10, 34));
$('#category').change(function() {
if ($(this).val() == 'restaurant') {
for (i = 0; i < restaurant.length; i++) {
marker = []
marker = new google.maps.Marker({
position: new google.maps.LatLng(restaurant[i][1], restaurant[i][2]),
map: map,
icon: restaurantMarker,
shadow: markerShadow,
animation: google.maps.Animation.DROP
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(restaurant[i][0]);
infowindow.open(map, marker);
};
})(marker, i));
}
}
if ($(this).val() == 'takeout') {
for (i = 0; i < takeout.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(takeout[i][1], takeout[i][2]),
map: map,
icon: takeoutMarker,
shadow: markerShadow,
animation: google.maps.Animation.DROP
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(takeout[i][0]);
infowindow.open(map, marker);
};
})(marker, i));
}
}
});
var restaurant = [
['<div class="restaurant"><strong>SoCo Southern Comfort</strong><br />509 Myrtle Ave. Brooklyn, NY 11205<br /> <a href="tel://718-783-1936">(718) 783-1936</a></div>', 40.693846,-73.964513, 1],
['<div class="restaurant"><strong>Black Swan</strong><br />1048 Bedford Ave. Brooklyn, NY 11205<br /><a href="tel://718-783-4744">(718) 783-4744</a></div>', 40.689128,-73.955187, 2]
];
var takeout = [
['<div class="take_out"><strong>Chan's Garden</strong><br />679 Myrtle Ave. Brooklyn, NY 11205<br /><a href="tel://718-875-6637">(718) 875-6637</a></div>', 40.694729,-73.956357, 1],
['<div class="take_out"><strong>New Hardee Chinese Restaurant</strong><br />835 DeKalb Ave. Brooklyn, NY 11221<br /><a href="tel://718-384-0338">(718) 384-0338</a></div>', 40.692559,-73.943021, 2]
];