简体   繁体   English

如何从谷歌地图v3删除标记?

[英]How to remove markers from google maps v3?

I am building a site which shows local places to get lunch. 我正在建立一个显示当地吃午饭的网站。 The site uses checkboxes to hide and show content:- 该网站使用复选框来隐藏和显示内容:

<li>Sandwich: <input type="checkbox" checked="checked" id="sandwichbox" onclick="boxclick(this,'sandwich')" /></li>
<li>Salad: <input type="checkbox" id="saladbox" onclick="boxclick(this,'salad')" /></li>
<li>Pasta: <input type="checkbox" id="pastabox" onclick="boxclick(this,'pasta')" /></li>

These are working fine, but I am using a switch statement to remove the markers. 这些工作正常,但是我正在使用switch语句删除标记。

function hide(category) {
switch(category) {
    case "sandwich":
      for (var i=0; i<gmarkers.length; i++) {
        if (gmarkers[i].sandwich == 1) {
          gmarkers[i].setVisible(false);
        }
      }
      break;
    case "salad":
      for (var i=0; i<gmarkers.length; i++) {
        if (gmarkers[i].salad == 1) {
          gmarkers[i].setVisible(false);
        }
      }
      break;
    case "pasta":
      for (var i=0; i<gmarkers.length; i++) {
        if (gmarkers[i].pasta == 1) {
          gmarkers[i].setVisible(false);
        }
      }
      break;
}
}

I am doing this because the shops can sell multiple things, (sandwiches and salad) for example, so each outlet cannot have a single category. 我这样做是因为商店可以出售多种商品(例如,三明治和沙拉),所以每个商店不能有一个类别。 So within my XML, there are nodes for each individual product :- 因此,在我的XML中,每个产品都有节点:-

<markers>
    <marker name="Earl of Sandwich" address="40 Ludgate Hill" tel="020 7236 2846" website="www.earlofsandwich.co.uk" lat="51.514072" lng="-0.101638" sandwich="1" salad="1" pasta="" oriental="" curry="" mexican="" jacket="" soup="1" sushi="" roast="" wraps="1" falafel=""/>
    <marker name="Chao! Now" address="4 St Andrews Hill" tel="02036320368" website="www.chaonow.co.uk" lat="51.513008" lng="-0.101322" sandwich="1" salad="" pasta="" oriental="1" curry="" mexican="" jacket="" soup="1" sushi="" roast="" wraps="" falafel=""/>
</markers>

What I would like to do is remove the switch statement and replace it with something like this, passing the category value into the for loop, but I can't seem to gte it to work:- 我想做的是删除switch语句,并将其替换为类似内容,将category值传递到for循环中,但是我似乎无法使其工作:-

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

I have tried the following:- 我尝试了以下方法:

function hide(category) {
    var mymarker = "gmarkers[i]." + category;
      for (var i=0; i<gmarkers.length; i++) {
        if (mymarker == 1) {
          gmarkers[i].setVisible(false);
        }
      }
}

But this is just passing mymarker in as a variable, and i is not assigned a value. 但这只是将mymarker作为变量传递,并且我没有分配值。

Any help is much appreciated, or if I am going down the wrong path and should be using a different format for my data, please point it out. 非常感谢您的帮助,或者如果我走错了方向并且应该为数据使用其他格式,请指出。 The other way I was considering was adding the values to an array and looping through that array, however, I only have a 1 or null value, and could not get a 2 dimensional array working using the category variable again. 我正在考虑的另一种方法是将值添加到数组中并遍历该数组,但是,我只有一个1或null值,并且无法再次使用category变量获取二维数组。

Thanks in advance, if you need any more info please let me know. 在此先感谢您,如果您需要更多信息,请告诉我。

I believe you want to access a object property using a variable. 我相信您想使用变量访问对象属性。 Use bracket notation instead of dot notation. 使用方括号符号代替点符号。 Say there is a object "person" 说有一个对象“人”

var person = {name: "someone", age: 23};

You can access the 'name' property like 您可以像访问“名称”属性

var name = person["name"]

You can also use bracket notation when the property name contains characters that would be either a syntax error or a keyword/reserved word. 当属性名称包含可能是语法错误或关键字/保留字的字符时,也可以使用括号表示法。 For example: 例如:

person["first name"] = "Nicholas";

It also allow variable name inside the the braces. 它也允许在括号内使用变量名。

person[propertyName] = "some value"; //propertyName is a variable

So in you case it should be 所以你的情况应该是

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

Hope this helps :) 希望这可以帮助 :)

If the category is a property of the marker, this should work: 如果类别是标记的属性,则这应该起作用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM