RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 619483
Accepted
ilya1099
ilya1099
Asked:2020-01-26 03:05:42 +0000 UTC2020-01-26 03:05:42 +0000 UTC 2020-01-26 03:05:42 +0000 UTC

当您在菜单中选择它们时,在 Google 地图中显示特定颜色的标签

  • 772

我试图通过选择菜单中的项目来显示该项目的标签,并删除所有其他标签,如果我删除该项目,则返回所有标签。编译了一个仅删除所选菜单图标的版本。帮助,谁了解如何使用 Google Maps API。

var map = {};

function initialize() {

  var mapProp = {
    center: {
      lat: -25.363,
      lng: 131.044
    },
    zoom: 8,
    markers: []
  };

  map = new google.maps.Map(document.getElementById("googleMap"), mapProp);

  var marker1 = new google.maps.Marker({
    position: {
      lat: -25.363,
      lng: 131.044
    },
    icon: 'http://maps.google.com/mapfiles/ms/icons/blue-dot.png',
    map: map,
    category: 'blue',
  });
  map.markers.push(marker1);
  var marker2 = new google.maps.Marker({
    position: {
      lat: -25.664,
      lng: 131.044
    },
    icon: 'http://maps.google.com/mapfiles/ms/icons/green-dot.png',
    map: map,
    category: 'green',
  });
  map.markers.push(marker2);
  var marker3 = new google.maps.Marker({
    position: {
      lat: -25.365,
      lng: 131.144
    },
    icon: 'http://maps.google.com/mapfiles/ms/icons/red-dot.png',
    map: map,
    category: 'red',
  });
  map.markers.push(marker3);
  var marker4 = new google.maps.Marker({
    position: {
      lat: -25.366,
      lng: 131.244
    },
    icon: 'http://maps.google.com/mapfiles/ms/icons/blue-dot.png',
    map: map,
    category: 'blue',
  });
  map.markers.push(marker4);
  var marker5 = new google.maps.Marker({
    position: {
      lat: -25.565,
      lng: 131.144
    },
    icon: 'http://maps.google.com/mapfiles/ms/icons/red-dot.png',
    map: map,
    category: 'red',
  });
  map.markers.push(marker5);
  var marker6 = new google.maps.Marker({
    position: {
      lat: -25.666,
      lng: 131.244
    },
    icon: 'http://maps.google.com/mapfiles/ms/icons/green-dot.png',
    map: map,
    category: 'green',
  });
  map.markers.push(marker6);

}
google.maps.event.addDomListener(window, 'load', initialize);


$(document).ready(function() {
  $('.markerBtn').bind('click', function(el) {
    var catToToggle = $(this).attr('data-category');
    $.each(map.markers, function() {
      if (this['category'] == catToToggle) {
        this.setVisible(!this.getVisible());
      }
    });
  });
});

.islands#redIcon {color: red;}
#name {
text-decoration: none;
}
<div id="googleMap" style="width:500px;height:380px; "></div>
<div class="markerBtn" data-category="green">
  <a href="#" id="name" style="color: green;">1 пункт</a>
</div>
<div class="markerBtn" data-category="red">
  <a href="#" id="name" style="color: red;">2 пункт</a>
</div>
<div class="markerBtn" data-category="blue">
  <a href="#" id="name" style="color: blue;">3 пункт</a>
</div>

javascript
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    cyadvert
    2020-02-04T05:01:49Z2020-02-04T05:01:49Z

    您的任务与GoogleMaps. 很干净javascript。

    首先,您需要存储每个菜单标签的“状态”(开/关)。
    其次,你应该注意里面的代码$.each(map.markers, function() {});。它会滚动所有可用的标记,您需要对其进行更改,以便在匹配时打开或关闭标记,如果不匹配则消失。

    功能getVisible()会setVisible()帮助你。
    第一个检查标记是否已启用(显示)。第二个打开或关闭它。

    这是您需要做的:
    首先,您需要为每个菜单项添加状态数据。像这样添加data-state="off":

    <div class="markerBtn" data-state="off" data-category="red">
    

    所有标签最初都是禁用的。

    好吧,改变js本身:

    $(document).ready(function() {
      $('.markerBtn').bind('click', function(el) {
        var catToToggle = $(this).attr('data-category'); /* категория нажатой метка */
        var catState = $(this).attr('data-state'); /* статус нажатой метки */
        /* сразу меняем статус нажатой метки. Было on - делаем off, и наоборот */
        if (catState=='on') {
          $(this).attr('data-state', 'off');
        } else {
          $(this).attr('data-state', 'on');
        }
        /* теперь проверяем все маркеры подряд и прячем или показываем в зависимости от категории нажатой метки и ее состояния */
        $.each(map.markers, function() {
          if (this['category'] == catToToggle) {
            if (catState=='on') { /* проверяем состояние метки */
              /* включена - показываем маркер */
              this.setVisible(true);
            } else {
              this.setVisible(false);
            }
          } else {
            this.setVisible(false); /* это маркер другой категории - прячем */
          }
        });
      });
    });
    

    直接插入代码的注释...

    注意:根据您拥有的 jQuery 版本,您.attr()很可能需要将.prop().

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    Python 3.6 - 安装 MySQL (Windows)

    • 1 个回答
  • Marko Smith

    C++ 编写程序“计算单个岛屿”。填充一个二维数组 12x12 0 和 1

    • 2 个回答
  • Marko Smith

    返回指针的函数

    • 1 个回答
  • Marko Smith

    我使用 django 管理面板添加图像,但它没有显示

    • 1 个回答
  • Marko Smith

    这些条目是什么意思,它们的完整等效项是什么样的

    • 2 个回答
  • Marko Smith

    浏览器仍然缓存文件数据

    • 1 个回答
  • Marko Smith

    在 Excel VBA 中激活工作表的问题

    • 3 个回答
  • Marko Smith

    为什么内置类型中包含复数而小数不包含?

    • 2 个回答
  • Marko Smith

    获得唯一途径

    • 3 个回答
  • Marko Smith

    告诉我一个像幻灯片一样创建滚动的库

    • 1 个回答
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Алексей Шиманский 如何以及通过什么方式来查找 Javascript 代码中的错误? 2020-08-03 00:21:37 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5