Google Maps V3标记管理器

时间:2011-05-30 10:09:11

标签: javascript google-maps markermanager

我正在努力让Marker Manager在Google Maps V3上工作。我试图复制教程页面here上显示的内容,但我似乎无法让它工作。这是我到目前为止使用的代码:

function gmapInit()
    {
        drawMap();
    }

    function drawMap()
    {
        var center = new google.maps.LatLng(..., ...);
        var mapOptions = {
            center: center,
            scrollwheel: false,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            disableDefaultUI: true,
            zoomControl: true,
            streetViewControl: true
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);


        var mgr = new MarkerManager(map);.
        google.maps.event.addListener(mgr, 'loaded', function()
        {
            createMarkers(map, mgr);
        });

        map.setCenter(center);
    }

    function createMarkers(map, mgr)
    {
        var bounds = new google.maps.LatLngBounds();

    <c:forEach var="place" items="${places}">
        var point = new google.maps.LatLng(..., ...);    
        bounds.extend(point);    

        var image = new google.maps.MarkerImage("...",               
                new google.maps.Size(25, 25),
                new google.maps.Point(0, 0),
                new google.maps.Point(0, 25),
                new google.maps.Size(25, 25));

        var marker = new google.maps.Marker({
            position: point,
            map: map,
            icon: image
        });

        google.maps.event.addListener(marker, 'click', function()
        {
           ...
        });           

        mgr.addMarker(marker, 20);

    </c:forEach>
        map.fitBounds(bounds);
        mgr.refresh();
        var listener = google.maps.event.addListener(map, "idle", function()
        {
            if (map.getZoom() > 16)
            {
                map.setZoom(16);
            }
        });
    }

然而,无论我放置什么值以及我放大的缩放级别,图标仍会继续显示。此外,我不断收到以下错误:

grid is undefined

对此有任何见解表示赞赏:)

我正在使用的MarkerManager文件可以找到here

2 个答案:

答案 0 :(得分:2)

这似乎解决了这个问题:

 var listener = google.maps.event.addListener(map, "idle", function()
        {
            if (map.getZoom() > 16)
            {
                map.setZoom(16);
            }
            mgr.refresh();
        });

问题是地图没有指定缩放级别。

答案 1 :(得分:1)

    var marker = new google.maps.Marker({
        position: point,
        map: map,
        icon: image
    });

    google.maps.event.addListener(marker, 'click', function()
    {
       ...
    });           

    mgr.addMarker(marker, 20);

    you should not assign map to marker while you are adding it to MarkerManager.

    var marker = new google.maps.Marker({
        position: point,
        **// map: map,**
        icon: image
    });

    google.maps.event.addListener(marker, 'click', function()
    {
       ...
    });           

    mgr.addMarker(marker, 20);