【问题标题】:(un)check button and show/hide markers google maps(取消)选中按钮并显示/隐藏标记谷歌地图
【发布时间】:2018-07-02 03:02:53
【问题描述】:

我尝试了与这个问题Check/uncheck all button to show/hide all markers Google maps api 几乎相同的事情

但我没有让它工作......我使用了相同的选中/取消选中按钮,但单击这些按钮不会改变我的标记。一次只选中/取消选中复选框。

我也尝试了“最初隐藏”的部分,但所有标记仍然可见..

你能帮帮我吗?我会很感激的!

这是我的代码:

<script type="text/javascript">
var gmarkers = [];
var map = null;
var ib = new InfoBox();

// Function to check/uncheck and show/hide all checkboxes and accordingly all markers
function check() {
  $('input[type="checkbox"]').prop("checked", true).change();
}

function uncheck() {
  $('input[type="checkbox"]').prop("checked", false).change();
}


// A function to create the marker and set up the event window
function createMarker(latlng,name,html,category) {
  var boxText = document.createElement("div");
  boxText.style.cssText = "margin-top: 10px; background: rgba(255,255,255,0.7); padding: 10px; border-radius: 10px; color: #000";
  boxText.innerHTML = html;

  var myOptions = {
    content: boxText,
    disableAutoPan: false,
    maxWidth: 0,
    pixelOffset: new google.maps.Size(-100, 0),
    zIndex: null,
    boxStyle: { 
      background: "url('tip.png') no-repeat",
      width: "250px",
    },
    closeBoxURL: "",
    infoBoxClearance: new google.maps.Size(1, 1),
    isHidden: false,
    pane: "floatPane",
    enableEventPropagation: false
  };

  var marker = new google.maps.Marker({
    position: latlng,
    icon: category + ".png",
    map: map,
    title: name,
    zIndex: Math.round(latlng.lat()*-100000)<<5
  });

  // === Store the category and name info as a marker properties ===
  marker.mycategory = category;                                 
  marker.myname = name;
  gmarkers.push(marker);

  google.maps.event.addListener(marker, 'click', function() {
    ib.setOptions(myOptions)
    ib.open(map, this);
  });
} // end createMarker

// == shows all markers of a particular category, and ensures the checkbox is checked ==
function show(category) {
  for (var i=0; i<gmarkers.length; i++) {
    if (gmarkers[i].mycategory == category) {
      gmarkers[i].setVisible(true);
    }
  }
  // == check the checkbox ==
  document.getElementById(category+"box").checked = true;
}

// == hides all markers of a particular category, and ensures the checkbox is cleared ==
function hide(category) {
  for (var i=0; i<gmarkers.length; i++) {
    if (gmarkers[i].mycategory == category) {
      gmarkers[i].setVisible(false);
    }
  }
  // == clear the checkbox ==
  document.getElementById(category+"box").checked = false;
  // == close the info window, in case its open on a marker that we just hid
  ib.close();
}

// == a checkbox has been clicked ==
 $(".checkbox").change(function() {
    var category = $(this).attr("value");
    // If checked
    if ($(this).is(":checked")) {
      show(category);
    } else {
      hide(category);
    }
  })



function myclick(i) {
  google.maps.event.trigger(gmarkers[i],"click");
}

function initialize() {
  var myOptions = {
    zoom: 14,
    center: new google.maps.LatLng(43.65623,-79.38518),
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    streetViewControl: false
  }
  map = new google.maps.Map(document.getElementById("map"), myOptions);

  google.maps.event.addListener(map, 'click', function() {
    ib.close();
  });

  $(document).ready(function(){
    $.getJSON('data.json', function(data) {
      for (var i = 0; i < data.length; i++) {
        // obtain the attribues of each marker
        var item = data[i];
        var point = new google.maps.LatLng(item.ltt, item.lgt);
        var name = item.name;
        var html = "<b>"+item.name+"<\/b><br \/>"+item.place+"<br \/><a href='"+ item.url +"' title='"+ item.name +"'>View details<\/a>";
        var category = item.cat;
        // create the marker
        var marker = createMarker(point,name,html,category);
      }



// == show or hide the categories initially ==
markers.forEach((marker) => marker.setVisible(false))

    }); //end JSON
  }); //end jQuery

} //end initialize

</script>
</head>
<body onload="initialize()"> 

<div class="checkbuttons">
        <input type="button" value="Check All" class="button" onclick="check();">
        <input type="button" value="Uncheck All" class="button" onclick="uncheck();"></div>
     <form action="#">
  <div class="map-check"><img src="eat.png" title="Eat" /><div><input type="checkbox" id="eatbox" value="eat" onclick="boxclick(this,'eat')" /> Places to Eat</div></div>
  <div class="map-check"><img src="stay.png" title="Stay" /><div><input type="checkbox" value="stay" id="staybox" onclick="boxclick(this,'stay')" /> Places to Stay</div></div>

    </div>
  </div> 
</div>

<div id="map" style="width: 750px; height: 600px"></div>
</body>

</html>

【问题讨论】:

  • 尝试发个jsfiddle,对你有帮助会更容易
  • 我已经尝试过了,但我的 data.json 无法正常工作。暂时小提琴是jsfiddle.net/9mgue1g6/2
  • Jsfiddle 对我不起作用,所以我在 codepen 上创建了一个示例并让 data.json 工作:codepen.io/anon/pen/qpwjpv
  • Justed 用解决方案更新了我的答案

标签: javascript jquery json api google-maps


【解决方案1】:

您的code 的问题在于没有调用应该触发显示/隐藏函数的change 回调。我在所有复选框类型的输入中添加了一个类markerTypeSwitch

<div class="checkbuttons">
  <input type="button" value="Check All" class="button" onclick="check();">
  <input type="button" value="Uncheck All" class="button" onclick="uncheck();"></div>
  <form action="#">
    <div class="map-check"><img src="eat.png" title="Eat" /><div><input class="markerTypeSwitch" type="checkbox" id="eatbox" value="eat" onclick="" checked/> Places to Eat</div></div>
    <div class="map-check"><img src="stay.png" title="Stay" /><div><input class="markerTypeSwitch" type="checkbox" id="staybox" value="stay" onclick="" checked/> Places to Stay</div></div>
    <div class="map-check"><img src="shop.png" title="Shop" /><div><input class="markerTypeSwitch" type="checkbox" id="shopbox" onclick="" value="shop" checked/>Places to Shop</div></div>
    <div class="map-check"><img src="play.png" title="Play" /><div><input class="markerTypeSwitch" type="checkbox" id="playbox" value="play" onclick="" checked/>Places to Play</div></div>
    <div class="map-check"><img src="community.png" title="Community" /><div><input  class="markerTypeSwitch" type="checkbox" value="community" id="communitybox" onclick="" checked/>Community Resources</div></div>
  </div>
</div> 

并将change事件附件更改为$(document).ready(function() {})

$(document).ready(function() {
    // == a checkbox has been changed ==
   $(".markerTypeSwitch").change(function() {
      var cat = $(this).attr("value");
      // If checked
      if ($(this).is(":checked")) {
        show(cat);
      } else {
        hide(cat);
      }
    });
});

查看codepen中的结果

【讨论】:

  • 是的!运行正常:(取消)检查类别有效,(取消)检查所有工作。我还解决了“最初隐藏”的问题:“gmarkers.forEach”而不是“markers.forEach”......(缺少'g')但现在问题是:第一次打开页面时所有标记都被隐藏(正如他们应该的那样),但所有复选框都被“选中”。更新笔:codepen.io/anon/pen/YYMxrN
  • 可能是因为输入中的“已检查”吗?
  • 已测试:将输入“已检查”更改为“未检查”。现在一切正常:codepen.io/anon/pen/WdWZLQTNX!! @克劳迪奥。
  • @photo1 太棒了!您应该删除unchecked,因为那不是有效的html。如果有效,请将答案标记为正确
  • 删除了这些部件,仍然可以正常工作。 Tnx 再次标记了你的答案,但可能没有显示,因为我是新手..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-01
相关资源
最近更新 更多