显示类别中的标记,单击时隐藏其余部分

时间:2015-09-21 13:19:44

标签: javascript jquery google-maps google-maps-api-3

我有一个类别列表

  • 酒店
  • 餐馆

点击酒店,显示此类别的标记。然后点击餐馆显示餐馆类别中的标记。当我点击例如时如何隐藏其他标记。餐馆吗?

 // == hide all markers of a particular category, and ensures the checkbox is checked ==
  function hide(category) {

    for (var i=0; i<gmarkers.length; i++) {
      if (gmarkers[i].mycategory == category) {
        gmarkers[i].setVisible(false);
      }
    }
  }


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

    }

    // == close the info window, in case its open on a marker that we just hid
    infowindow.close();
  }

  // == a checkbox has been clicked ==
  function boxclick(box,category) {
    if (box.checked) {
      hide(category);


    } else {
      show(category);
    }
    // == rebuild the side bar
    makeSidebar();
  }

2 个答案:

答案 0 :(得分:1)

您只需将地图设置为function show(category) { for (var i=0; i<gmarkers.length; i++) { if (gmarkers[i].mycategory == category) { gmarkers[i].setMap(mymap); } else { gmarkers[i].setMap(null); } } } function hide(category) { for (var i=0; i<gmarkers.length; i++) { if (gmarkers[i].mycategory == category) { gmarkers[i].setMap(null); } else { gmarkers[i].setMap(mymap); } } } 即可。所以你会写一些类似的东西:

mymap

首次加载文档时,您必须为地图保存变量,例如变量string CutString(string input) { Match result = Regex.Match(input, @"[a-zA-Z]+[0-9]+"); return result.Value; }

答案 1 :(得分:0)

我找到了一个简单的解决方案:)

function showhide(category) {
    for (var i=0; i<gmarkers.length; i++) {
  if (gmarkers[i].mycategory == category) {
    gmarkers[i].setVisible(true);
  } else {
    gmarkers[i].setVisible(false);
  }
}

    makeSidebar();
  }

全部