“marker.setVisible(false)"和“marker.setVisible(false)"有什么区别?和“marker.setMap(null)"在谷歌地图 v3 中? [英] What is the difference between "marker.setVisible(false)" and "marker.setMap(null)" in Google Maps v3?
问题描述
我想清除 Google 地图上的标记.
I want to clear a marker on Google Maps.
marker.setVisible(false)
和 marker.setMap(null)
有什么区别?
但我不知道,哪个是对的?
But I don't know, which is right?
推荐答案
这两种方法之间的区别似乎没有明确记录.但是,请注意以下几点:
The difference between the two methods does not seem to be clearly documented. However, note the following:
当您使用
setMap(null)
时,您的标记将丢失对Map
的引用.如果您不保留对Map
对象的引用,您将无法重新显示标记.
When you use
setMap(null)
, your marker will lose the reference to theMap
. If you do not keep a reference to theMap
object, you wouldn't be able to reshow the marker.
此外,setMap()
方法不会触发 visible_changed
事件,而 setVisible()
方法会触发 (如果可见性实际上已切换).
In addition, the setMap()
method will not trigger the visible_changed
event, while the setVisible()
method does (if the visibility is actually toggled).
示例:
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: new google.maps.LatLng(-25.363, 131.044),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
position: new google.maps.LatLng(-25.363, 131.044),
map: map
});
google.maps.event.addListener(marker, 'visible_changed', function() {
console.log('visible_changed triggered');
});
marker.setVisible(false); // visible_changed triggered
marker.setVisible(true); // visible_changed triggered
marker.setMap(null); // visible_changed not triggered
marker.setMap(map); // visible_changed not triggered
我想当我们打算在地图上再次显示标记时,我们应该使用 setVisible(false)
方法,而当我们打算重新显示标记时,应该使用 setMap(null)
不会再显示它了.
I guess we should be using the setVisible(false)
method when we intend to reshow the marker again on the map, and the setMap(null)
when we will not be showing it again.
这篇关于“marker.setVisible(false)"和“marker.setVisible(false)"有什么区别?和“marker.setMap(null)"在谷歌地图 v3 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!