Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Marker Visibility In Google Maps

I'm building a treasure hunter app and I need to be able to hide a marker and only make it visible at a certain zoom level.

How do I achieve this?

I'm using a custom marker and google maps v3.

Thanks.

Oh and what's weird is that I can turn the visibility off at a certain zoom level like in the following code:

var marker = new google.maps.Marker({
    draggable: false,
    raiseOnDrag: false,
    clickable: true,
    icon: image,
    shadow: shadow,
    shape: shape,
    map: map,
    url: 'http://www.google.com/',
    visible: true,
    position: markerLatlng
});

var zoomLevel;
//marker.visible = false;

google.maps.event.addListener(marker, 'click', function() {
    window.location.href = marker.url;
});

var infowindow = new google.maps.InfoWindow(
{
    content: 'Oh You Found Me!!!',
    size: new google.maps.Size(25,25),
    position: myLatlng
});


google.maps.event.addListener(map, 'zoom_changed', function() {
    zoomLevel = map.getZoom();

    if (zoomLevel == 16) {

        marker.visible = false;

        infowindow.open(map,marker);

    }
});

but if I reverse the marker.visibility such that:

var marker = new google.maps.Marker({

    draggable: false,

    raiseOnDrag: false,

    clickable: true,

    icon: image,

    shadow: shadow,

    shape: shape,

    map: map,

    url: 'http://www.google.com/',



    visible: false,

    position: markerLatlng

});

google.maps.event.addListener(map, 'zoom_changed', function() {
    zoomLevel = map.getZoom();

    if (zoomLevel == 16) {

        marker.visible = true;

        infowindow.open(map,marker);

    }      
});

The marker won't show up on the map at all.

like image 916
Patrick McCreanor Avatar asked Feb 17 '11 14:02

Patrick McCreanor


People also ask

Can you 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.

What do the markers on Google Maps mean?

A marker identifies a location on a map. By default, a marker uses a standard image. Markers can display custom images, in which case they are usually referred to as "icons." Markers and icons are objects of type Marker . You can set a custom icon within the marker's constructor, or by calling setIcon() on the marker.

What are the markers on Google Maps 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".


1 Answers

The proper way of setVisible is marker.setVisible(false);

like image 179
KJYe.Name Avatar answered Oct 05 '22 23:10

KJYe.Name