2012-02-23 15 views
0

まず、このウェブサイトに貢献してくれた皆様に感謝します。私は専門家ではありませんが、このコミュニティから多くのことを学びました:)GoogleマップV3 - マーカーを削除してクラスタを更新する問題

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に設定するという受け入れられた方法であると思われるものを含め、多くのことを試みましたが、この状況では私にとってはうまくいきません。スクリプトを削除すると、マーカー変数

答えて

0

私は、変数マーカースコープはグローバルではありませんので、これはでなければならないと思うので、私はここにあなたの問題はあなたが単に

を表示しないようにマーカーを設定しているということだと思います

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

これはprobabます:私はあなたもそうのように、マーカーの配列から個々のマーカーを削除するには、いくつかのロジックを追加する必要があると思う

markers[i].setMap(null); 

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

また、種類ごとに1つのMarkerClustererの複数のインスタンスを管理することもできます。次に、あなただけの

markerCluster.clearMarkers(); 

ボーナス(opt_textColor & opt_anchor)を呼び出すことができます。 私はあなたの変数は単に "の、textColor" & "アンカー" である必要があります信じています。オブジェクト(オブジェクト):

  • 'スタイル'スタイルプロパティ: ...
    • 'anchor':(配列)ラベルテキストのアンカー位置。
    • 'textColor' :(文字列)テキストの色です。
0

にアクセスすることができないとき

関連する問題