放大和缩小后计算地图上显示的标记

时间:2012-05-05 01:07:46

标签: google-maps-api-3

我正在使用google-maps-utility-library-v3。 我正在尝试放大和缩小,如果点不在屏幕中,我想看到计数0。 但它不起作用。如果我注释掉mgr.addMarkers(markers,5);它有效....但它总是0,因为没有制造商由经理管理。 有人可以告诉我为什么吗?

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
  html { height: 100% }
  body { height: 100%; margin: 0; padding: 0 }
  #map_canvas { height: 100% }
</style>
<script type="text/javascript"
  src="http://maps.googleapis.com/maps/api/js?key=??????????????&sensor=false">
</script>
<script type="text/javascript" src="downloadxml.js"></script>
<script src="http://google-maps-utility-library-v3.googlecode.com/svn/tags/markermanager/1.0/src/markermanager.js"></script>
<script type="text/javascript">

  var map = null;
  function initialize() {

    var myOptions = {
        zoom: 8,
        center: new google.maps.LatLng(42.35428, -71.05525),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    map = new google.maps.Map(document.getElementById('map'), myOptions);

    //hard code a marker
    var lat = 42.35428;
    var lng = -71.05525;
    var point = new google.maps.LatLng(lat,lng);
    var html = "I am a hard-coded dot";
    var marker = new google.maps.Marker({
        map: map,
        position: point,
        icon: "http://www.google.com/mapfiles/arrow.png",
        shadow: "http://www.google.com/mapfiles/arrowshadow.png"
    });
    var markers = [];
    markers.push(marker);
    var mgr = new MarkerManager(map);
    mgr.addMarkers(markers,5);
    mgr.refresh();

    google.maps.event.addListener(map, 'zoom_changed', function() {
        var zoomLevel = map.getZoom();
        var count = mgr.getMarkerCount(zoomLevel);
        document.getElementById("Listing").innerHTML += count + "<BR>";
    });
  }

</script>

               

1 个答案:

答案 0 :(得分:1)

在完全初始化之前,您似乎正在使用MarkerManager。以下是修复此问题的代码的更新部分:

var markers = [];
markers.push(marker);
var mgr = new MarkerManager(map);
google.maps.event.addListenerOnce(mgr, 'loaded', function(){
    mgr.addMarkers(markers, 5);
    mgr.refresh();
});

请参阅此示例作为参考:http://google-maps-utility-library-v3.googlecode.com/svn/tags/markermanager/1.0/docs/examples.html