【问题标题】:I want to hide map markers while in street view. (Google maps v3)我想在街景中隐藏地图标记。 (谷歌地图 v3)
【发布时间】:2015-05-07 04:58:45
【问题描述】:

当用户将小黄人拖到屏幕上时,它会转换为全屏街景。这很好。但是,它也用我的地图标记在屏幕上乱扔垃圾。这很糟糕。

解决这个问题的困难方法是循环遍历地图上所有可能的标记,存储在启动街景之前可见的标记,然后在街景完成后恢复它们。但我在 API 文档的任何地方都找不到街景启动事件。

更好、更简单的解决方案是设置一些选项,例如“在街景中不显示我的标记”。

编辑:设法找到监控街景状态的方法。 How to detect enter and exit streetView in Google Maps API v3

但是,我仍然想知道是否有办法让...在街景中不显示标记。

最终编辑: 我包括“解决方案”。这是我最终编写的简化版本,因为每个层都涉及整个应用程序中的各个模块,因此它们的处理并不像所示的那样简单。 最终解决方案:

// Add listener for when we go into street view.
  google.maps.event.addListener(map.getStreetView(), 'visible_changed', function() {
    var setAllMap = function (mapObjects, state) {
      for (var i = 0, len = mapObjects.length; i < len; i++) {
        mapObjects[i].setMap(state);
      }
    };

    // If street view was just activated
    if (this.getVisible()) {
      // hide everything
      setAllMap(LayerOne, null);
      setAllMap(LayerTwo, null);
      setAllMap(LayerThree, null);

    // If we're leaving street view
    } else {
      // show the marker layers that were on

      setAllMap(LayerTwo, true);
      setAllMap(LayerThree, true);

      // If visibility state was true
      if (LayerOne.getVisible()) {
        setAllMap(LayerOne, true);
      }

      if (LayerTwo.getVisible()) {
        setAllMap(LayerTwo, true);
      }

      if (LayerThree.getVisible()) {
        setAllMap(LayerThree, true);
      }
    }
  });

【问题讨论】:

    标签: google-maps-api-3 google-maps-markers google-street-view


    【解决方案1】:

    使用您发布的答案中的侦听器,查看街景何时处于活动状态并执行setAllMap(null); 以隐藏标记。当它离开街景时,请setAllMap(map); 再次显示标记。

    编辑(来自 Google 地图文档):

    function setAllMap(map) {
      for (var i = 0; i < markers.length; i++) {
        markers[i].setMap(map);
      }
    }
    

    【讨论】:

    • 这是假设setAllMap 是我定义的函数吗?我发现的唯一函数是我定义的函数,它基本上会遍历数组并在每个函数上调用 someMarkerList[i].setMap(null)。
    • 抱歉,忘记包含我引用的链接:developers.google.com/maps/documentation/javascript/examples/…
    • 啊!它有点复杂的原因是因为我正在处理的应用程序具有各种独立的包含标记的层,每个层都有自己的逻辑。所以我基本上必须为多达 6 个不同的地图对象图层制作一个 setmap,然后存储打开的图层,并在街景会话结束后恢复它们。
    • 感谢您的回答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 2021-01-31
    • 1970-01-01
    • 2015-10-09
    相关资源
    最近更新 更多