显示网页上地图上的标记数量

时间:2012-10-23 14:06:01

标签: javascript

我想在嵌入式地图下方的网页上添加一行文字,说明当前地图上有多少针......“此地图目前有XX个针脚。” 我想让XX自动填写当前号码。这就是标记信息的存储方式..

  var locations = [
    ['  place a ',  21.81821, 63.84583, 0.000000  ],
    ['  place b ',  34.56343, 67.89073, 0.000000  ]];

和标记创建并聚集在这里

var markers = [];

  var marker, i;

     for (i = 0; i < locations.length; i++) {  
     marker = new google.maps.Marker({
     position: new google.maps.LatLng(locations[i][1], locations[i][2]),
     map: map
     });

     google.maps.event.addListener(marker, 'click', (function(marker, i) {
     return function() {
     infowindow.setContent(locations[i][0]);
     infowindow.open(map, marker);
     }})(marker, i));
       markers.push(marker)}

  var markerCluster = new MarkerClusterer(map, markers,{gridSize:40});}
     google.maps.event.addDomListener(window, 'load', initialize);

我想也许document.write可以自动调用元素的长度(位置/标记/标记...),如下所示

.
.
var XX = locations.length

<div id="map"></div>
<script language="javascript" type="text/javascript">
document.write("the map currently has", [XX], "pins");
</script>

但我无法实现它,使用示例位置,读出会说“地图当前有 2 引脚”

2 个答案:

答案 0 :(得分:0)

MarkerClusterer有一个getTotalMarkers()函数,可以返回群集中的标记总数 - 无需重新发明轮子...

如果我理解正确,工作(未经测试,我会尽快检查):

//assumes an element with an id of marker-counter that you want to output to
document.getElementById("marker-counter").innerHTML = "the map currently has" + markerCluster.getTotalMarkers() + "pins";

更新:链接已更新为指向MarkerClusterer文档而非MarkerManager

答案 1 :(得分:0)

你试过了吗?

document.write("the map currently has " + XX + " pins");

我认为没有理由不这样做。