【发布时间】:2013-06-20 11:17:09
【问题描述】:
我设置了一个包含 122 个标记、多个类别 (12) 的 Google 地图。我可以根据每个标记使用的图像创建一个过滤器,以便打开/关闭表单中的标记吗? 将另一个变量定义为“类别”变量会更好吗? 如果我使用 JQuery,我该如何重构代码以使其正常工作?
任何想法都将不胜感激。
JScript 看起来像这样:
function initialize() {
var myOptions = {
center: new google.maps.LatLng(,),
zoom: 12,
mapTypeId: google.maps.MapTypeId.TERRAIN
};
var map = new google.maps.Map(document.getElementById('map_canvas'),myOptions);
var image = []; //define an array to store category images
image['church']='icons/chapel-2.png'
image['monastery']='icons/convent-2.png'
image['archaeo']='icons/monument.png'
image['wild']='icons/wildlife.png'
image['museum']='icons/museum_openair.png'
image['beach']='icons/beach.png'
image['must']='icons/star.png'
image['summit']='icons/peak.png'
image['cave']='icons/cave-2.png'
image['forest']='icons/palmTree.png'
image['gorge']='icons/canyon-2.png'
image['village']='icons/smallcity.png'
//define 122 markers as below until var marker122 (no comments here I am trying to keep it simple..
var marker = new google.maps.Marker({
position: new google.maps.LatLng(,),
map: map,
title: 'placeName',
clickable: true,
icon: image['must']
});
HTML 看起来像这样:
<form action="#">
Must Visit: <input type="checkbox" id="mustbox" onclick="boxclick(this,'must')" />
Beaches: <input type="checkbox" id="beachbox" onclick="boxclick(this,'beach')" />
Archaeology: <input type="checkbox" id="archaeobox" onclick="boxclick(this,'archaeo')" />
Church: <input type="checkbox" id="religionnbox" onclick="boxclick(this,'church')" />
Monastery: <input type="checkbox" id="conventbox" onclick="boxclick(this,'convent')" />
Gorge: <input type="checkbox" id="gorgebox" onclick="boxclick(this,'gorge')" />
Cave: <input type="checkbox" id="cavetbox" onclick="boxclick(this,'cave')" />
Forest: <input type="checkbox" id="forestbox" onclick="boxclick(this,'forest')" />
Wildlife: <input type="checkbox" id="wildbox" onclick="boxclick(this,'wild')" />
Museum: <input type="checkbox" id="museumbox" onclick="boxclick(this,'museum')" />
Villages: <input type="checkbox" id="villagebox" onclick="boxclick(this,'village')" /><br />
Mountain Summit: <input type="checkbox" id="summitbox" onclick="boxclick(this,'summit')" /><br />
【问题讨论】:
-
另外,我刚刚通过简单的搜索找到了这个示例,并附有 jsfiddle 示例:stackoverflow.com/questions/11682069/…
-
@dKen 我的表格状态好吗?因为我将标记存储在分类数组中,所以我不应该在我的表单中写:onclick="boxclick(this,'ArrayName')"??
-
你能把你拥有的所有东西都添加到 JSFiddle 中,我来看看吗?将它放在 JSFiddle 中意味着我不必查看与问题无关的代码,而且还可以帮助您在构建问题时解决问题。
-
@dKen link
-
感谢您的链接。很抱歉让您感到痛苦,但其中有超过 1,000 行代码;你能删除除 2 个类别和 5 个标记之外的所有类别以显示问题所在吗?您的链接也不会加载 Google 地图;它会为你加载吗?
标签: javascript html checkbox