Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 814
simon Avatar asked May 23 '11 17:05

simon


People also ask

How do I hide markers on Google Maps?

Step 1 Go to Add or Edit Map and Scroll down to the 'Infowindow Settings' section. Step 2 Enable the box of 'Hide Markers on Page Load' option. Step 3 Click on Save Map and open it in browser.

How do you inspect element on a map?

To do this you really need to be using a browser like Chrome, Firefox, or Opera. Once you have the page open in those browsers, the quickest way to open the inspector is to right-click somewhere on the page and select Inspect (on Chrome) or Inspect Element (Firefox and Opera).

What is the Google Maps marker called?

The Google Maps pin is the inverted-drop-shaped icon that marks locations in Google Maps. The pin is protected under a U.S. design patent as "teardrop-shaped marker icon including a shadow". Google has used the pin in various graphics, games, and promotional materials.


1 Answers

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!

like image 109
eyecatchUp Avatar answered Sep 21 '22 02:09

eyecatchUp