过滤标记Google Maps API V3

时间:2011-12-29 12:00:53

标签: javascript google-maps-api-3

我需要你的帮助:

目前,我在Google地图电子表格中有一组数据。我需要几件事来过滤这些数据。例如,类别和类型。

这些是使用JSON收集的:

/** 
 * Called when JSON is loaded. Creates sidebar if param_sideBar is true.
 * Sorts rows if param_rankColumn is valid column. Iterates through worksheet rows, 
 * creating marker and sidebar entries for each row.
 * @param {JSON} json Worksheet feed
 */       
function cm_loadMapJSON(json) {

  var bounds = new google.maps.LatLngBounds();

  for (var i = 0; i < json.feed.entry.length; i++) {
    var entry = json.feed.entry[i];
    if(entry["gsx$" + param_latColumn]) {
      var lat = parseFloat(entry["gsx$" + param_latColumn].$t);
      var lng = parseFloat(entry["gsx$" + param_lngColumn].$t);
      var point = new google.maps.LatLng(lat,lng);
      var html = "<div class='infoscherm' style='font-size:12px'>";
      html += "<h2>" + entry["gsx$"+param_titleColumn].$t 
              + "</h2>";
      var label = entry["gsx$"+param_titleColumn].$t;
      if(entry["gsx$" + param_screenCategory]) {
        html += "<h3>" + "Screen Category/Purpose: " + "</h3>" + "<p>" + entry["gsx$"+param_screenCategory].$t + "</p>";
      }
      var screentype = entry["gsx$"+param_screenType].$t;
      if(entry["gsx$" + param_screenType]) {
        html +=  "<h3>" + "Screen Type: " + "</h3>" + "<p>" + entry["gsx$"+param_screenType].$t + "</p>";
      }
      if(entry["gsx$" + param_publicSpace]) {
        html += "<h3>" + "Type of Public Space: " + "</h3>" + "<p>" + entry["gsx$"+param_publicSpace].$t + "</p>";
      }
      var space = entry["gsx$"+param_publicSpace].$t;
      if(entry["gsx$" + param_screenInteraction]) {
        html += "<h3>" + "Type of interaction: " + "</h3>" + "<p>" + entry["gsx$"+param_screenInteraction].$t + "</p>";
      }
      if(entry["gsx$" + param_descriptionColumn]) {
        html += "<h3>" + "Description: " + "</h3>" + "<p>" + entry["gsx$"+param_descriptionColumn].$t + "</p>";
      }
      if(entry["gsx$" + param_screenAddress]) {
        html += "<h3>" + "Screen Location: " + "</h3>" + "<p>" + entry["gsx$"+param_screenAddress].$t + "</p>";
      }
      html += "</div>";

      // create the marker
      var marker = cm_createMarker(kaart,point,label,html,screentype,space);
      // cm_map.addOverlay(marker);
      cm_mapMarkers.push(marker);
      cm_mapHTMLS.push(html);
      bounds.extend(point); 
    }
  }

  kaart.fitBounds(bounds);
  kaart.setCenter(bounds.getCenter());
}

现在我希望能够过滤位于数组中的标记。因此,只显示适合过滤器的标记。

此功能可从地图中删除所有标记。我正在考虑修改此功能,以便为标记创建一个良好的工作动态过滤器。但我无法弄明白。任何帮助?或者想法甚至可能是其他更好的工作方式?

function clearOverlays() {
  if (cm_mapMarkers) {
    for (i in cm_mapMarkers) {
      cm_mapMarkers[i].setMap(null);
    }
  }
}

Thanx提前

1 个答案:

答案 0 :(得分:0)

您需要并行数组或键值数组,例如:

cm_mapMarkers.push({ point: marker, category: 'something' });

然后

function showSome(categoryName) {
  if (cm_mapMarkers) {
    for (var i = 0, marker; marker = cm_mapMarkers[++i]; ) {
      if(marker.category == categoryName){
        // do something with this marker
      }else{
        // do or do not with others.
      }
    }
  }
}