Javascript Google Maps V3:检查地图上是否存在标记?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6100514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:14:13  来源:igfitidea点击:

Google Maps V3: Check if marker is present on map?

javascriptgoogle-mapsgoogle-maps-api-3

提问by simon

In Google Maps V3, is there a way to check whether a marker is actually present on the map?

在 Google Maps V3 中,有没有办法检查地图上是否确实存在标记?

I have markers that vanish when clicked. I'd like some logic to check the present visibility of the marker.

我有点击时消失的标记。我想要一些逻辑来检查标记的当前可见性。

For example:

例如:

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));

I was hoping this would give me a null type when the marker isn't visible, but in fact it's still an Object.

我希望当标记不可见时这会给我一个空类型,但实际上它仍然是一个对象。

So, how else can I check whether this particular marker is visible on the map?

那么,我还能如何检查此特定标记在地图上是否可见?

Thanks!

谢谢!

回答by eyecatchUp

This one-liner will return trueif the marker's position is containedunder the current mapboundary, and return falseif not.

true如果marker's 的位置包含在当前map边界下,则此单行将返回,否则返回false

map.getBounds().contains(marker.getPosition())

Hope that helps, Cheers!

希望有帮助,干杯!

回答by Jasoon

start_marker.getMap()

Would return null if you'd previously used start_marker.setMap(null); as in your example.

如果您以前使用过 start_marker.setMap(null),则会返回 null;就像你的例子一样。

That said, why not use setVisible and getVisible if you just want to hide and show markers?

也就是说,如果您只想隐藏和显示标记,为什么不使用 setVisible 和 getVisible 呢?

回答by ambay

If you wish to just to hide/show the marker you could use the setVisible method of of the marker like:

如果您只想隐藏/显示标记,您可以使用标记的 setVisible 方法,例如:

 start_marker.setVisible(false);//to hide
 start_marker.setVisible(true);//to show

because setMap(null) does not hide the marker but removes the marker from the map.

因为 setMap(null) 不会隐藏标记而是从地图中删除标记。

You could then use getVisible() to get the visibility of the marker like:

然后,您可以使用 getVisible() 来获取标记的可见性,例如:

console.log('Type of start_marker is now: ' + start_marker.getVisible());

You could read them here: https://developers.google.com/maps/documentation/javascript/overlays#Markershttps://developers.google.com/maps/documentation/javascript/overlays

你可以在这里阅读:https: //developers.google.com/maps/documentation/javascript/overlays#Markers https://developers.google.com/maps/documentation/javascript/overlays

回答by no.good.at.coding

回答by Argiropoulos Stavros

I think you have to change your logic.Why not store your markers in an array and remove them completely from this array when they are clicked.So the remaining markers are the visible ones.

我认为你必须改变你的逻辑。为什么不把你的标记存储在一个数组中,并在点击它们时从这个数组中完全删除它们。所以剩下的标记是可见的。

Cheers

干杯