2011-07-19 333 views
1

我有一個設置,允許我在基於複選框打開和關閉的情況下,成功過濾Google地圖中的標記。雖然我的新挑戰是某些標記屬於多個類別,但使我的設置不正確。我在學習,但這似乎超出了我。如何修改我的設置以僅使用過濾器將每個標記解除限制爲1個類別?每個標記最多可以屬於5個類別,並且如果選中相應的複選框,則需要顯示該標記。感謝您對此提供的任何幫助。谷歌地圖標記和多個類別的過濾標記

我的JS是:

< script type = "text/javascript" > 
    //<![CDATA[ 
    if (GBrowserIsCompatible()) { 
     var gmarkers = []; 
     var gicons = []; 
     var baseIcon = new GIcon(G_DEFAULT_ICON); 
     baseIcon.iconAnchor = new GPoint(9, 34); 
     baseIcon.iconSize = new GSize(20, 34); 
     baseIcon.infoWindowAnchor = new GPoint(9, 2); 
     //gicons["theatre"] = new GIcon(baseIcon,"colour086.png"); 
     //gicons["golf"] = new GIcon(baseIcon,"colour108.png"); 
     // gicons["info"] = new GIcon(baseIcon,"colour125.png"); 
     // A function to create the marker and set up the event window 

     function createMarker(point, name, html, category) { 
      var marker = new GMarker(point, gicons[category]); 
      // === Store the category and name info as a marker properties === 
      marker.mycategory = category; 
      marker.myname = name; 
      GEvent.addListener(marker, "click", function() { 
       marker.openInfoWindowHtml(html); 
      }); 
      gmarkers.push(marker); 
      return marker; 
     } 
     // == 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].show(); 
       } 
      } 
      // == 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].hide(); 
       } 
      } 
      // == clear the checkbox == 
      document.getElementById(category + "box").checked = false; 
      // == close the info window, in case its open on a marker that we just hid 
      map.closeInfoWindow(); 
     } 
     // == a checkbox has been clicked == 

     function boxclick(box, category) { 
      if (box.checked) { 
       show(category); 
      } 
      else { 
       hide(category); 
      } 
      // == rebuild the side bar 
      makeSidebar(); 
     } 

     function myclick(i) { 
      GEvent.trigger(gmarkers[i], "click"); 
     } 
     // == rebuilds the sidebar to match the markers currently displayed == 

     function makeSidebar() { 
      var html = ""; 
      for (var i = 0; i < gmarkers.length; i++) { 
       if (!gmarkers[i].isHidden()) { 
        html += '<a href="javascript:myclick(' + i + ')">' + gmarkers[i].myname + '<\/a><br>'; 
       } 
      } 
      document.getElementById("side_bar").innerHTML = html; 
     } 
     // create the map 
     var map = new GMap2(document.getElementById("map_canvas")); 
     map.addControl(new GLargeMapControl()); 
     map.addControl(new GMapTypeControl()); 
     map.setCenter(new GLatLng(40.734953, -73.86775399999999), 8); 
     // Read the data 
     GDownloadUrl("/locator/testxml/11106/5000", function (doc) { 
      var xmlDoc = GXml.parse(doc); 
      var markers = xmlDoc.documentElement.getElementsByTagName("marker"); 
      for (var i = 0; i < markers.length; i++) { 
       // obtain the attribues of each marker 
       var lat = parseFloat(markers[i].getAttribute("lat")); 
       var lng = parseFloat(markers[i].getAttribute("lng")); 
       var point = new GLatLng(lat, lng); 
       var address = markers[i].getAttribute("address"); 
       var name = markers[i].getAttribute("name"); 
       var html = "<b>" + name + "<\/b><p>" + address; 
       var category = markers[i].getAttribute("category"); 
       // create the marker 
       var marker = createMarker(point, name, html, category); 
       map.addOverlay(marker); 
      } 
      // == show or hide the categories initially == 
      show("young_men"); 
      show("juniors"); 
      show("girls"); 
      show("boys"); 
      show("toddler"); 
      // == create the initial sidebar == 
      makeSidebar(); 
     }); 
    } 
    else { 
     alert("Sorry, the Google Maps API is not compatible with this browser"); 
    } 
    //]]> 
    < /script> 

然後將HTML看起來這樣:

<form id="geofinder_form" method="post" action="index.php" > 
    </div> 
    <div class="form-line"> 
    <div class="form-checkbox"> 
     <input type = "checkbox" id="young_menbox" onclick="boxclick(this,'young_men')" /> 
     <span>Young Men's</span></div> 
    <div class="form-checkbox"> 
     <input type = "checkbox" id="juniorsbox" onclick="boxclick(this,'juniors')" /> 
     <span>Junior's</span></div> 
    <div class="form-checkbox"> 
     <input type = "checkbox" id="girlsbox" onclick="boxclick(this,'girls')" /> 
     <span>Girls</span></div> 
    <div class="form-checkbox"> 
     <input type = "checkbox" id="boysbox" onclick="boxclick(this,'boys')" /> 
     <span>Boys</span></div> 
    <div class="form-checkbox"> 
     <input type = "checkbox" id="toddlerbox" onclick="boxclick(this,'toddler')" /> 
     <span>Toddler/Infant</span></div> 
    </div> 
    <div class="form-line"> 
    <div class="form-input"><span>Zip-code</span> 
     <input type="text" class="zipcode" name="geoquery" onblur="if (!this.value) this.value = 'Zipcode'" onclick="this.value = ''" value="Zipcode" /> 
    </div> 
    <div class="form-miles"> 
     <select name="radius" id="select-radius" style="width:200px;"> 
     <option value="5">Radius</option> 
     <option value="5">5 miles</option> 
     <option value="10">10 miles</option> 
     <option value="15">15 miles</option> 
     <option value="20">20 miles</option> 
     <option value="100">25 + miles</option> 
     </select> 
    </div> 
    </div> 
    <br clear="all" /> 
    <button class="small-button" type="submit">SEARCH!</button> 
</form> 

這是用來顯示標記的XML文件中的一個片段:

<markers> 
<marker name="Sample Store Name" address="92-59 59th, City, ST 11109" lng="-73.86775399999999" lat="40.734953" category="young_men"/> 
</markers> 

回答

0

比方說樣本商店名稱標記所屬類別: 年輕人後輩。 XML文件可以進行如下更新:

<markers> <marker name="Sample Store Name" address="92-59 59th, City, ST 11109" lng="-73.86775399999999" lat="40.734953" category="young_men,juniors"/> </markers>

所以JS文件是這樣的:

... 
var category = markers[i].getAttribute("category"); 
var gcategory = new Array(); 
gcategory = category.split(","); 
// create the marker 
     for (var j = 0; j < gcategory.length; j++) { 
     var category = gcategory[j]; 
     var html = "<b>"+name+"<\/b><p>"+address; 
     var marker = createMarker(point,name,html,category); 
     map.addOverlay(marker); 
     }