0

情報ウィンドウが表示されますが、何らかの理由で同じマーカーを複数クリックすると同じマーカーを複数クリックしたときに表示されます。私は自分のコードで何かをしなければならないと感じていますが、私はそれが何であるかに私の指を置いていません。どんな助けもありがとうございます。同じマーカーを2回クリックすると2つのInfoWindowが開きます

var map; 
var markers = []; 

function initMap() { 
    map = new google.maps.Map(document.getElementById('map_canvas'), { 
     zoom: 14, 
     center: new google.maps.LatLng(33.6894120, -117.9872660), 
     mapTypeId: 'roadmap', 
     disableDefaultUI: true 
    }); 



    function addMarker(feature) { 
     var marker = new google.maps.Marker({ 
      position: feature.position, 
      icon: icons[feature.type].icon, 
      map: map, 
      type: feature.type, 
      title: feature.title, 
      description: feature.description 
     }); 
     marker.addListener('click', function() { 
      map.setCenter(marker.getPosition()); 

      var infoWindow = new google.maps.InfoWindow({ 
       map: map, 
       pixelOffset: new google.maps.Size(0, -60) 
      }); 
      infoWindow.setContent(marker.description); 
      infoWindow.setPosition(marker.position); 

      google.maps.event.addListener(map, 'drag', function() { 
       infoWindow.close(); 
      }); 
      google.maps.event.addListener(map, 'click', function() { 
       infoWindow.close(); 
      }); 
     }); 
     markers.push(marker); 
    } 

    filterMarkers = function(getType) { 
     //console.log(getType); 
     for (var i = 0; i < markers.length; i++) { 
      if (markers[i].type == getType || getType == "") { 
       markers[i].setVisible(true); 
      } else { 
       markers[i].setVisible(false); 
      } 
     } 
    } 

    var features = [ 

     { 
      position: new google.maps.LatLng(-33.91721, 151.22630), 
      type: 'type1', 
      description: 'Description1' 
     },{ 
      position: new google.maps.LatLng(-33.91721, 151.22630), 
      type: 'type2', 
      description: 'Description2' 
     },{ 
      position: new google.maps.LatLng(-33.91721, 151.22630), 
      type: 'type3', 
      description: 'Description3' 
     } 


    ]; 

    for (var i = 0, feature; feature = features[i]; i++) { 
     addMarker(feature); 
    } 
} 

$(document).ready(function(){ 
    initMap(); 
}); 
+0

あなたはあなたのコードの多くを与えることはできますか?マーカ変数はどこで作成しますか? –

+0

forループを追加しました。何かもっと必要ですか? – oompahlumpa

+0

私が言っているのは、あなたがマーカー配列に向かっているのですが、どこに定義したのかわかりません。これはおそらくあなたがこのエラーを起こしている理由です。 –

答えて

0

あなたはすべてのマーカークリックで新しいInfoWindowを作成している:

marker.addListener('click' ... var infoWindow = *new google.maps.InfoWindow(...*

あなたが複数のインスタンスを取得することが期待されます。

あなたはすべてのマーカーに1 InfoWindowをしたい場合は、各マーカーにつき1はthis SO answerをチェックアウトしたい場合は、this example

に従うことができます。

+0

両方の例では、単一のコンテンツ文字列を使用しているようです。自分のコンテンツ・ストリングをマーカーの配列に定義できるようにしたい。 – oompahlumpa

2

マーカーをクリックするたびにインフォウィンドウが作成されないようにするには、マーカーをクリックするたびに新しい情報ウィンドウを作成し、マーカー用に1つ作成します。 1つだけ開いてほしい)、クリックリスナで開きます。

function addMarker(feature) { 
    var marker = new google.maps.Marker({ 
    position: feature.position, 
    map: map, 
    type: feature.type, 
    description: feature.description 
    }); 
    // create infowindow for the marker 
    var infoWindow = new google.maps.InfoWindow({}); 
    marker.addListener('click', function() { 
    map.setCenter(marker.getPosition()); 
    // set the content of the infowindow 
    infoWindow.setContent(marker.description); 
    // open the infowindow on the marker. 
    infoWindow.open(map,marker); 
    }); 
    markers.push(marker); 
} 

proof of concept fiddle

関連する問題