Javascript Google 地图中的标记可见性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5030127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:19:50  来源:igfitidea点击:

Marker Visibility In Google Maps

javascriptgoogle-mapsgoogle-maps-markers

提问by Patrick McCreanor

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.

我正在使用自定义标记和谷歌地图 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:

但是如果我反转了marker.visibility,这样:

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.

标记根本不会出现在地图上。

回答by u476945

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

setVisible 的正确方法是 marker.setVisible(false);

回答by Jonathan

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, // <------
});