0

我得到了infowindows的工作,但由於某種原因,如果我點擊相同的標記多次點擊,它會打開多個相同的infowindow。我有一種感覺,它必須是我的代碼的東西,但我不能把我的手指放在它是什麼。任何幫助表示讚賞。點擊相同的標記兩次打開兩個InfoWindows

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(...*

預計你會得到多個實例。

如果你想要一個InfoWindow所有標記,你可以按照this example

如果你想有每個每個標記一個檢查this SO answer

+0

這兩個示例似乎都使用單個內容字符串。我希望能夠在我的標記數組中定義我的內容字符串。 – oompahlumpa

2

如果您不希望每次點擊標記時創建一個infowindow,請不要在每次點擊標記時創建一個新的,爲標記創建一個(或者爲地圖創建一個,如果您只想要一個打開),並在點擊監聽器中打開它。

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

相關問題