31

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!

simon
  • 5,987
  • 13
  • 31
  • 28
  • 1
    PS: yes, one way is to set `start_marker=null` myself, but I'm curious to know whether there's an inbuilt way! – simon May 23 '11 at 17:14
  • marker is there and its an object, its just not bound to a/the map. – Salman A May 23 '11 at 17:18
  • 1
    By "visible on the map", do you mean that the marker is within the user's viewport? Or do you mean just that the marker has been placed on the map, whether or not it is outside the bounds of what the user has zoomed in on? – Trott May 23 '11 at 17:21
  • 1
    Trott's questions is very relevant and determines which of the answers is correct. – Ryan Olds May 23 '11 at 17:55

5 Answers5

72

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())
starball
  • 20,030
  • 7
  • 43
  • 238
eyecatchUp
  • 10,032
  • 4
  • 55
  • 65
  • @Stavros: The most common event that triggers vp changes is the drag handler. So you have a very high chance of repeating points within your map's bb after vp changes. Thus it's a very bad idea to 'remove' once created objects, for the moment you don't need them, as the chance is really high, you will need them after the next dragend again. And as DOM calls are really 'expensive', you should use them wisley. Indeed - in most cases - it is the best way to keep things, since you are (relativly) sure, you will not need them again later on. – eyecatchUp Jun 17 '11 at 21:32
  • 3
    [contains](https://developers.google.com/maps/documentation/javascript/reference#LatLngBounds) just says it **"returns true if the given lat/lng is in this bounds"** and doesn't actually tell you if an existing marker had already been place on the map. – johntrepreneur Jan 11 '13 at 22:04
11
start_marker.getMap()

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

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

Jasoon
  • 432
  • 2
  • 9
10

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

 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.

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

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

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

cristy
  • 317
  • 3
  • 9
  • 48 hours later, I found the real solution to hiding and showing map markers. When working with map clusters, setMap() doesn't hide the markers on bounds change. This preserves it. – Spencer May Nov 10 '16 at 15:11
5

Marker#getVisible() ?

no.good.at.coding
  • 20,221
  • 2
  • 60
  • 51
0

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

Argiropoulos Stavros
  • 9,436
  • 11
  • 61
  • 79