3

MarkerClusterer v3とビューポートマーカー管理の両方を正常にセットアップしました(ビューポートで表示されるマーカーのみを収集するためにajax呼び出しを実行し、マップが「アイドル」の場合は常にそれらをレンダリングします)。

ただし、これらを組み合わせると、ページが最初に読み込まれたときにのみ機能し、その後は機能しないように見えます。

ズームまたはパンすると、最初のクラスターが残り、マップ全体のマーカーはクラスター化されていない状態でレンダリングされますが、以前にクラスター化されたマーカーは残ります。

元のクラスター化されたマーカーは、ズームイン/ズームアウトしても正しく動作しますが、ビューポートの境界が変更されたときに提供される新しいマーカーは、それらに追加またはクラスター化されません。

以下のコード:

function drawMap(swLat, swLng, neLat, neLng){
    //Load the map data
     $.ajax({
            type: "POST",
            url: "readMapInfo.php",
            cache: false,
            data:{S:swLat, W:swLng, N:neLat, E:neLng},
            dataType: "xml",
            success: function(data) {
            if(markerArray.length >0){
                for (i in markerArray) {
                    markerArray[i].setMap(null);
                }
                drawMarker(data);   //takes the info provided and performs "markerArray.push(marker);"
                mc = new MarkerClusterer(map, markerArray, clusterOptions);
            } else {
                drawMarker(data);   //takes the info provided and performs "markerArray.push(marker);"
                mc = new MarkerClusterer(map, markerArray, clusterOptions);
            }
    });
}

google.maps.event.addListener(map, 'idle', function() {
    bounds = map.getBounds();
    sw = bounds.getSouthWest();
    ne = bounds.getNorthEast();
    swLat = sw.lat();
    swLng = sw.lng();
    neLat = ne.lat();
    neLng = ne.lng();

    drawMap(swLat, swLng, neLat, neLng);
});
4

1 に答える 1

2

問題の説明は詳細かつ完全ですが、問題を示すページへの URL もあれば簡単です。もちろん、この特定のシナリオではそれが不可能な場合があります。そうは言っても、私は手伝ってくれるでしょう:

ajax 成功コールバックの最初に、追加のクリーンアップ コードが必要だと思います。

if( markerArray.length > 0 ) {
    // For loop logic is unchanged
    // It removes the markers from the Map, but leaves them in markerArray
    for (i in markerArray) {
        markerArray[i].setMap( null );    
    }

    // New code to truncate the Array; the markers should be removed
    markerArray.length = 0;

    // New code to clear the clusterer; the markers should be removed
    mc.clearMarkers();        

    // Original line of code unchanged
    drawMarker(data);   //takes the data and performs markerArray.push(marker)

    // Commented out, because the cluster is cleared each time, not recreated
    //mc = new MarkerClusterer(map, markerArray, clusterOptions);

    // New code to refill the cluster, rather than recreate the cluster
    // The original clusterOptions are retained
    mc.addMarkers( markerArray );

} else {
    // Original line of code unchanged
    drawMarker(data);   //takes the data and performs markerArray.push(marker)

    // Original line of code unchanged
    // Necessary here, because the clusterer does not yet exist
    mc = new MarkerClusterer(map, markerArray, clusterOptions);
}

これがあなたを前進させるのに役立つと信じています。これで問題が解決するか、少なくとも役立つかどうかをお知らせください。

当面の課題を解決したら、 MarkerClustererPlusを確認することもお勧めします。それは質問で説明されています: Is there any way to disable the Marker Clusterer for less than defined marker counts? .

于 2012-05-02T03:05:46.223 に答える