Javascript Appendchild为每条记录创建一个Count

时间:2011-08-01 13:21:29

标签: javascript google-maps

我正在使用Google Maps Javascript来创建地图。我需要计算已创建的每个标记的计数,并将此计数放在DIV中。 scipt收集计数,但它显示每个标记的计数。 例如,如果从查询返回三个标记,则3次获得echod三次。如果创建四十三个标记,则数字43在DIV中回复四十三次。

var MarkerCount;
var count = markerNodes.length;
var markercountEntry = createMarkerCount(count);
MarkerCount.appendChild(markercountEntry);

function createMarkerCount(count) {
    var div = document.createElement('div');
    var html = "<br/>" + count;
    div.innerHTML = html;
    div.style.marginBottom = '5px'; 
    return div;
}

感谢任何帮助,以帮助理解我为什么会遇到这种行为。预期的行为是将结果写入一次,而不是x次,具体取决于有多少标记。

编辑------ 这是完整的代码:

var icon = new google.maps.MarkerImage("./images/plum_flag.png", 
new google.maps.Size(35, 52), new google.maps.Point(0, 0), 
new google.maps.Point(0, 52));
var map;
var markers = [];
var infoWindow;
var sidebar;
var MarkerCount;

function load() {
  map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(36.1611, -116.4775),
    zoom: 6,
    mapTypeId: 'roadmap',
    mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DEFAULT}
  });
  infoWindow = new google.maps.InfoWindow();
  sidebar = document.getElementById("sidebar");
  }

 function formatPhone(telephone) { 
return "("+telephone.substring(0,3)+")"+telephone.substring(3,6)+"-"+telephone.substring(6,11); 
}


 function searchLocations() {
 var address = document.getElementById("addressInput").value;
 var geocoder = new google.maps.Geocoder();
 geocoder.geocode({address: address}, function(results, status) {
   if (status == google.maps.GeocoderStatus.OK) {
    searchLocationsNear(results[0].geometry.location);
   } else {
     alert(address + ' not found');
  }
  });
}

 function clearLocations() {
 infoWindow.close();
 for (var i = 0; i < markers.length; i++) {
   markers[i].setMap(null);
 }
 markers.length = 0; 
 sidebar.innerHTML = "";
 map.setCenter(new google.maps.LatLng(36.1611, -116.4775), 6);

}

 function searchLocationsNear(center) {
 clearLocations(); 

 var state = document.getElementById('stateSelect').value;
 var facility = document.getElementById('facility').value;
 var searchUrl = 'sql_to_xml.asp?facility=' + facility + '&state=' + state; 
 downloadUrl(searchUrl, function(data) { 
   var xml = parseXml(data);
   var markerNodes = xml.documentElement.getElementsByTagName("marker");
   var bounds = new google.maps.LatLngBounds();

var sidebar = document.getElementById('sidebar');
   sidebar.innerHTML = '';
   if (markerNodes.length == 0) {
     sidebar.innerHTML = 'No results found.';
     map.setCenter(new google.maps.LatLng(36.1611, -116.4775), 6);
     return;
   }

   var MarkerCount = document.getElementById('MarkerCount');
   MarkerCount.innerHTML = '';
   if (markerNodes.length == 0) {
     MarkerCount.innerHTML = 'No results found.';
     map.setCenter(new google.maps.LatLng(36.1611, -116.4775), 6);
     return;
   }

   for (var i = 0; i < markerNodes.length; i++) {
     var count = markerNodes.length;
     for (i=0;i<count;i++) 
     {     
     var markercountEntry = createMarkerCount(i);     
     MarkerCount.appendChild(markercountEntry); 
     } 
     var name = markerNodes[i].getAttribute("name"); 
     var address = markerNodes[i].getAttribute("address"); 
     var city = markerNodes[i].getAttribute("city");
     var state = markerNodes[i].getAttribute("state");
     var zipcode = markerNodes[i].getAttribute("zipcode");
     var telephone = markerNodes[i].getAttribute("telephone"); 
     var images = markerNodes[i].getAttribute("images"); 
     var url = markerNodes[i].getAttribute("url");
     var latlng = new google.maps.LatLng( 
          parseFloat(markerNodes[i].getAttribute("lat")), 
          parseFloat(markerNodes[i].getAttribute("lng")));

     var marker = createMarker(latlng, name, address, city, state, zipcode, telephone, images, url);
     bounds.extend(latlng);
     var sidebarEntry = createSidebarEntry(marker, name, address, city, state, zipcode, telephone, images, url);
     sidebar.appendChild(sidebarEntry);
     //var markercountEntry = createMarkerCount(count);
     //MarkerCount.appendChild(markercountEntry);
     }

     map.fitBounds(bounds);

     });
}

function createMarker(latlng, name, address, city, state, zipcode, telephone, images, url) { 
  var html = "<div id='infodiv'>" + name + "<br/><h4>" + address + "<br/>" + city + ", " + state + " " + zipcode + "<br/>" + (formatPhone(telephone)) + "</h4><div class='infoimage'><img src='" + images + "'></div><div class='footer'><a href='http://" + url + "'>" + url + "</a></div></div>";
  var marker = new google.maps.Marker({
    icon: icon, 
    map: map, 
    position: latlng 
  });
  markers.push(marker);

  google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent(html);
    infoWindow.open(map, marker);
  });
  return marker;
}

function createSidebarEntry(marker, name, address, city, state, zipcode, telephone, images, url) {
  var div = document.createElement('div');
  var html = "<ul class='column'><li><div class='block'><a href='http://" + url + "'>" + name + "</a><br/>" + address + "<br/>" + city + ", " + state + " " + zipcode + "<br/>" + (formatPhone(telephone)) + "</div></li></ul>";
  div.innerHTML = html;
  div.style.marginBottom = '5px'; 
  return div;
}

 function createMarkerCount(count) {
  var div = document.createElement('div');
  var html = "<br/>" + count;
  div.innerHTML = html;
  div.style.marginBottom = '5px'; 
  return div;
}

function downloadUrl(url, callback) {
  var request = window.ActiveXObject ?
      new ActiveXObject('Microsoft.XMLHTTP') :
      new XMLHttpRequest;

  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      request.onreadystatechange = doNothing;
      callback(request.responseText, request.status);
    }
  };

  request.open('GET', url, true);
  request.send(null);
}

function parseXml(str) {
  if (window.ActiveXObject) {
    var doc = new ActiveXObject('Microsoft.XMLDOM');
    doc.loadXML(str);
    return doc;
  } else if (window.DOMParser) {
    return (new DOMParser).parseFromString(str, 'text/xml');
  }
}

function doNothing() {}

1 个答案:

答案 0 :(得分:2)

不要将createMarkerCount放入循环中!

所以,改变:

   for (var i = 0; i < markerNodes.length; i++) {
     var count = markerNodes.length;
     for (i=0;i<count;i++) 
     {     
     var markercountEntry = createMarkerCount(i);     
     MarkerCount.appendChild(markercountEntry); 
     } 
     var name = markerNodes[i].getAttribute("name"); 
     var address = markerNodes[i].getAttribute("address"); 
     var city = markerNodes[i].getAttribute("city");
     var state = markerNodes[i].getAttribute("state");
     var zipcode = markerNodes[i].getAttribute("zipcode");
     var telephone = markerNodes[i].getAttribute("telephone"); 
     var images = markerNodes[i].getAttribute("images"); 
     var url = markerNodes[i].getAttribute("url");
     var latlng = new google.maps.LatLng( 
          parseFloat(markerNodes[i].getAttribute("lat")), 
          parseFloat(markerNodes[i].getAttribute("lng")));

     var marker = createMarker(latlng, name, address, city, state, zipcode, telephone, images, url);
     bounds.extend(latlng);
     var sidebarEntry = createSidebarEntry(marker, name, address, city, state, zipcode, telephone, images, url);
     sidebar.appendChild(sidebarEntry);
     //var markercountEntry = createMarkerCount(count);
     //MarkerCount.appendChild(markercountEntry);
     }

     map.fitBounds(bounds);

     });
}

var markercountEntry = createMarkerCount(markerNodes.length);     
MarkerCount.appendChild(markercountEntry); 
   for (var i = 0; i < markerNodes.length; i++) {
     var name = markerNodes[i].getAttribute("name"); 
     var address = markerNodes[i].getAttribute("address"); 
     var city = markerNodes[i].getAttribute("city");
     var state = markerNodes[i].getAttribute("state");
     var zipcode = markerNodes[i].getAttribute("zipcode");
     var telephone = markerNodes[i].getAttribute("telephone"); 
     var images = markerNodes[i].getAttribute("images"); 
     var url = markerNodes[i].getAttribute("url");
     var latlng = new google.maps.LatLng( 
          parseFloat(markerNodes[i].getAttribute("lat")), 
          parseFloat(markerNodes[i].getAttribute("lng")));

     var marker = createMarker(latlng, name, address, city, state, zipcode, telephone, images, url);
     bounds.extend(latlng);
     var sidebarEntry = createSidebarEntry(marker, name, address, city, state, zipcode, telephone, images, url);
     sidebar.appendChild(sidebarEntry);
     //var markercountEntry = createMarkerCount(count);
     //MarkerCount.appendChild(markercountEntry);
     }

     map.fitBounds(bounds);

     });
}