Google地图V3:检查地图上是否存在标记? [英] Google Maps V3: Check if marker is present on map?

查看:96
本文介绍了Google地图V3:检查地图上是否存在标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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);
});
// ...稍后在代码中:检查标记当前是否可见。
console.log('start_marker的类型现在是:'+ typeof(start_marker));

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



那么,我该如何检查这个特定的标记是否可以在地图上看到?

谢谢! / p>

解决方案

如果在当前 map 边界下,c> marker 的位置是 contains ,并返回 false 如果不是。

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

希望有帮助,干杯!


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

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!

解决方案

This one-liner will return true if the marker's position is contained under the current map boundary, and return false if not.

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

Hope that helps, Cheers!

这篇关于Google地图V3:检查地图上是否存在标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆