我可以在 map 上显示许多不同类别的位置。最终我想对它们应用过滤器。这个场景可能非常熟悉,因为我在尝试解决问题时在网上发现了很多这样的场景。我把标记放在 map 上,但找不到隐藏它们的方法。这是我的尝试方法:
function addLocations($content, id, map){
var $mapdiv = $content.find('div.map_div');
catValues = [map.catPrimary, map.catWhite, map.catGreen, map.catYellow, map.catRed, map.catBrown, map.catPurple, map.catGray, map.catOrange];
db.locations.all(function(obj){
$.each(obj, function(index, location){
if(location.value.nodeID == id){
var latitude = location.value.latitude;
var longitude = location.value.longitude;
var description = location.value.description;
var category = location.value.category;
var position = new google.maps.LatLng(latitude, longitude);
if(category == "0"){
homeLocation = position;
$mapdiv.gmap('get','map').setOptions({'center':position});
}
var marker = new google.maps.Marker({
position: position,
icon: "assets/img/marker_" + category + ".png",
category: category,
shadow: iconShadow,
});
marker.setMap( $mapdiv.gmap('get','map') );
$mapdiv.gmap('addMarker', marker).click(function() {
$mapdiv.gmap('openInfoWindow', { 'content': description + "<br/> (" + catValues[category] + ")"}, this);
});
}
});
markers = $mapdiv.gmap('get', 'markers');
for(var i = 0; i<markers.length; i++){
if(markers[i].category != "0"){
//"not primary, hiding
markers[i].setVisibile(false);
}
}
});
所有标记都会显示,而那些应该隐藏的标记则不会。在将标记添加到 map 并使用它们之前,我还尝试将标记添加到数组中,但没有成功。当我在 try 周围包含标记[i].setVisible(false) 并捕获它时,它显示“对象#没有方法'setVisible'。令人惊讶的是,如果我测试标记[i].getVisible(),我会得到值true。 感谢您提前提供的帮助。
最佳答案
我相信正确的代码是markers[i].setMap(null);
https://developers.google.com/maps/documentation/javascript/overlays#RemovingOverlays
摘自文章:
Removing Overlays
To remove an overlay from a map, call the overlay's setMap() method, passing null. Note that calling this method does not delete the overlay; it simply removes the overlay from the map. If instead you wish to delete the overlay, you should remove it from the map, and then set the overlay itself to null.
If you wish to manage a set of overlays, you should create an array to hold the overlays. Using this array, you can then call setMap() on each overlay in the array when you need to remove them. (Note that unlike in V2, no clearOverlays() method exists; you are responsible for keeping track of your overlays and removing them from the map when not needed.) You can delete the overlays by removing them from the map and then setting the array's length to 0, which removes all references to the overlays.
关于javascript - 无法从 GMap v3 隐藏标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12395319/