Marker Visibility In Google Maps

谁说我不能喝 提交于 2019-12-23 06:59:46

问题


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.


回答1:


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




回答2:


If you want to set the visibility on initialisation of the marker instead of as a method after-the-fact, use the following syntax (see the docs):

marker = new google.maps.Marker({
    map: map,
    visible: false, // <------
});


来源:https://stackoverflow.com/questions/5030127/marker-visibility-in-google-maps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!