【问题标题】:google map multiple markers simple filtering谷歌地图多个标记简单过滤
【发布时间】: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')" /> &nbsp;&nbsp;
            Beaches: <input type="checkbox" id="beachbox" onclick="boxclick(this,'beach')" /> &nbsp;&nbsp;
            Archaeology: <input type="checkbox" id="archaeobox" onclick="boxclick(this,'archaeo')" /> &nbsp;&nbsp;
            Church: <input type="checkbox" id="religionnbox" onclick="boxclick(this,'church')" /> &nbsp;&nbsp;
        Monastery: <input type="checkbox" id="conventbox" onclick="boxclick(this,'convent')" /> &nbsp;&nbsp;
        Gorge: <input type="checkbox" id="gorgebox" onclick="boxclick(this,'gorge')" /> &nbsp;&nbsp;
            Cave: <input type="checkbox" id="cavetbox" onclick="boxclick(this,'cave')" /> &nbsp;&nbsp;
            Forest: <input type="checkbox" id="forestbox" onclick="boxclick(this,'forest')" /> &nbsp;&nbsp;
            Wildlife: <input type="checkbox" id="wildbox" onclick="boxclick(this,'wild')" /> &nbsp;&nbsp;
            Museum: <input type="checkbox" id="museumbox" onclick="boxclick(this,'museum')" /> &nbsp;&nbsp;
        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


【解决方案1】:

您可以在多个地图标记上创建过滤器,它会非常有效地工作。我已经在我的一个项目中做到了。如果有一个下拉作为过滤器,则通过将显示和隐藏设置为其可见性属性,将下拉过滤器的选定值应用于地图制作者对象。 为此,您需要在 javascript 中使用 collection(array)。 在开始时,您将所有 google 标记对象添加到集合中。选择任何过滤器后,只需从该集合中更改地图标记对象的属性,即可更改可见性、图像及其其他属性等属性。你会看到它会非常有效地工作。避免在选择过滤器时创建新对象,这会给地图带来不平滑。

【讨论】:

    【解决方案2】:

    当然。我做了一些非常相似的事情,但我现在找不到确切的项目。我通过将标记添加到数组中,并循环访问这些数组并根据单击的复选框显示/隐藏它们来做到这一点。

    // Create an array
    var markersFacebook = [];
    
    // Create a marker
    var marker = new google.maps.Marker({
         position: new google.maps.LatLng(,), 
         map: map,
         title: 'placeName',
         clickable: true,
         icon: image['must']
    });
    
    // Add the marker to an array of your choice. You'll have one for each category
    markersFacebook.push(marker);
    
    // Now you can show and hide your markers by looping through the array
    function toggleMarkers(visible)
    {
        jQuery(markersFacebook).each(function(id, marker) {
            // Hide or show the marker here depending on the state of a checkbox, or whatever you like
            marker.setVisible(visible);
        });
    }
    
    toggleMarkers(false); // This should hide all the markers
    

    抱歉,我无法为您提供一个完整的、有效的示例,但数组方法应该适用于您想要实现的目标。

    【讨论】:

    • 我应该一次在数组中添加一个标记还是可以使用这样的东西:markersMust.push(marker, marker117, marker118, marker119, marker120, marker121);标记Church.push(标记28,标记29,标记30,标记31,标记32,标记33,标记34);或者是否可以在可能的情况下使用连续范围?并且关于 JQuery 是这样的:function toggleMarkers(visible){ jQuery(markersMust).each(function(mustbox, marker) { marker.setVisible(visible); marker117.setVisible(visible); marker118.setVisible(visible); marker121.setVisible(visible);}}
    • 我相信我已经为我的类别创建了数组并将标记存储在它们各自的数组中。我的问题是 jquery 似乎没有连接到我的“复选框”表单才能正常工作。有没有办法让您浏览代码以亲自查看?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 2016-10-14
    • 2021-02-22
    相关资源
    最近更新 更多