0

まず、このウェブサイトに貢献してくれた皆さんに感謝します。私は専門家ではありませんが、このコミュニティから多くのことを学びました:)

ユーザーが特定の領域のカテゴリをフィルタリングし、選択されているカテゴリに応じてマーカーをマップに追加できるカスタムメニューを備えたGoogleマップがあります。マーカーは、表示されると「マーカー」と呼ばれる配列に追加されます。すべてが正常に機能しているように見えましたが、markercluster.jsプラグインを追加すると、問題が発生し始めました。カテゴリのマーカーがマップから削除されても、マーカー配列からは削除されません。マーカーが削除されても更新されず、マーカーが追加された場合にのみ更新されるため、これはクラスター番号にノックオン効果があるようです。

        // The navigaiton item that has been clicked 
        $(source + ' .map-controls ul li a').live('click', function(e){
            // The category of the clicked item eg. hikes, kayaking etc
            var markerClass = $(this).attr('class');
            // If the clicked items is not visible on the map
            if(!$(this).parent().hasClass('visible')) {
                // Go through JSON to find matching categories
                for(i=0; i<category.length; i++) {
                    // If we find a match to the category of the clicked item
                    if(category[i].mapmarker == markerClass){
                        // Grab the latitude and longitude
                        var lat = category[i].lat;
                        var long = category[i].long;
                        // Create a position variable
                        var myLatlng = new google.maps.LatLng(lat,long);
                        latslongs.push(myLatlng);
                        // Create the marker
                        var marker = new google.maps.Marker({
                            position: myLatlng,
                            map: map
                        });
                        // Give the marker and id of the category clicked
                        marker.setValues({type: "point", id: markerClass});
                        // Set the category to the category clicked
                        marker.mycategory = markerClass;
                        // Push the marker to the markers array
                        markers.push(marker);           
                    }
                }
                // Marker Cluster options - Bonus point if you can tell me why opt_textColor and opt_anchor don't work?
                mcOptions = {styles: [{
                    opt_textColor: 'white',
                    height: 47,
                    url: "http://localhost/img/bgs/bg-marker-cluster.png",
                    width: 46,
                    opt_anchor: [5, 12]
                }]}
                // Set up MarkerCluster
                var markerCluster = new MarkerClusterer(map, markers, mcOptions);
                // Make the markers visible on the map
                $(this).parent().addClass('visible');

            } else {
                    // ELSE - If the clicked categories markers are visible when clicked, we go through the array of all the markers currently visible on the map
                    for (i=0; i < markers.length; i++) {
                        // If we find a match to the clicked category
                        if(markers[i].get('id') == markerClass){
                            // HERE IS WHERE I HAVE MY PROBLEM!!
                            // This line works as in it removes the correct markers from the map, but not from the markers array.
                            // I've seen suggestions of markers.lenght = 0 but I can't do that as I have others markers on the map
                            markers[i].setMap(null);
                            // I only found this out when adding the cluster plugin. Before it seemed to be working perfectly but 
                            // I had no idea the array just kept growing in size while showing/hiding different categories.
                            // I have tried so many things to try and remove the correct array items from the array, but I can't get it.
                            // The other issue is that the cluster numbers are not updating when removing categories??? But they get higher
                            // everytime you add a category. This might be a knock on effect of the ever growing markers array.
                        }
                    }
                    // Make the markers invisible on the map
                    $(this).parent().removeClass('visible');

            }
            // Thanks guys and girls.
            e.preventDefault();

        });

    },

どんな助けでも大歓迎です。google apiを使用するのはこれが初めてなので、少し迷ってしまい、アドバイスやアドバイスが欲しいです。マーカー配列を0に設定するための受け入れられた方法を含む多くのことを試しましたが、この状況ではうまくいきません。

4

2 に答える 2

0

ここでの問題は、単にマーカーを表示しないように設定していることだと思います

markers[i].setMap(null);

次のように、マーカーの配列から個々のマーカーを削除するロジックも追加する必要があると思います。

//remove the marker from the map
markers[i].setMap(null);
//remove the marker from the array
markers.splice(i, 1);

それらをメモリ内で追跡したい場合は、おそらく別の配列でそれらを管理する必要があります。

または、タイプごとに 1 つずつ、MarkerClusterer の複数のインスタンスを管理することもできます。それからあなたはただ電話することができます

markerCluster.clearMarkers();

おまけ (opt_textColor & opt_anchor): 変数は "textColor" & "anchor" である必要があると思います:

  • 'styles': (オブジェクト) スタイル プロパティを持つオブジェクト: ...
    • 'anchor': (配列) ラベル テキストのアンカー位置。
    • 'textColor': (文字列) テキストの色。
于 2012-03-19T17:51:10.710 に答える
0

これは、変数マーカーのスコープがグローバルではないため、スクリプトを削除するとマーカー変数にアクセスできないためだと思います

于 2012-02-23T12:10:48.253 に答える