sim*_*mon 25 javascript google-maps google-maps-api-3
在Google Maps V3中,有没有办法检查地图上是否确实存在标记?
点击时我的标记消失了.我想要一些逻辑来检查标记的当前可见性.
例如:
var start_marker = null;
start_marker = new google.maps.Marker({ position: location, map: map, clickable: true });
google.maps.event.addListener(start_marker, 'click', function(event) {
start_marker.setMap(null);
});
// ... Later in code: check whether marker is currently visible.
console.log('Type of start_marker is now: ' + typeof(start_marker));
Run Code Online (Sandbox Code Playgroud)
当标记不可见时,我希望这会给我一个null类型,但实际上它仍然是一个Object.
那么,我怎样才能检查这个特定的标记在地图上是否可见?
谢谢!
eye*_*hUp 60
true如果marker位置包含在当前map边界下,则该单行将返回,否则返回false.
map.getBounds().contains(marker.getPosition())
Run Code Online (Sandbox Code Playgroud)
希望有所帮助,干杯!
start_marker.getMap()
Run Code Online (Sandbox Code Playgroud)
如果您之前使用过start_marker.setMap(null),则返回null; 在你的例子中.
也就是说,如果你只想隐藏和显示标记,为什么不使用setVisible和getVisible呢?
小智 9
如果您只想隐藏/显示标记,可以使用标记的setVisible方法,如:
start_marker.setVisible(false);//to hide
start_marker.setVisible(true);//to show
Run Code Online (Sandbox Code Playgroud)
因为setMap(null)不会隐藏标记但会从地图中删除标记.
然后,您可以使用getVisible()来获取标记的可见性,如:
console.log('Type of start_marker is now: ' + start_marker.getVisible());
Run Code Online (Sandbox Code Playgroud)
您可以在此处阅读:https : //developers.google.com/maps/documentation/javascript/overlays#Markers https://developers.google.com/maps/documentation/javascript/overlays