2011-11-03 188 views
1

我正在製作一張具有不同標記的地圖,應該有不同的信息窗口。但是當我試圖將一個infowindow放在多個標記上時,infowindow的位置停留在第一個點擊標記上。我究竟做錯了什麼?谷歌地圖api v3:infowindow的位置

<script type="text/javascript"> 
     function initialize() { 
     var latlng = new google.maps.LatLng(53.033117,5.639752); 

     var latingSiton = new google.maps.LatLng(53.0356197730161,5.62613738302669); 
     var SitonImg = 'images/siton.png'; 
     var latingZee = new google.maps.LatLng(53.0360534900284,5.62488617774811); 
     var ZeeImg = 'images/zee.png'; 
     var latingHylkema = new google.maps.LatLng(53.0373853769166,5.62924255777811); 
     var HylkemaImg = 'images/hylkema.png'; 
     var latingDijkstra = new google.maps.LatLng(53.0360125088934,5.62735529113307); 
     var DijkstraImg = 'images/dijkstra.png'; 

     var myOptions = { 
      zoom: 16, 
      center: latlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 

     var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

     var contentString = 'test'; 

     var infowindow = new google.maps.InfoWindow({ 
      content: contentString 
     }); 

      var marker = new google.maps.Marker({ 
       position: latingSiton, 
       map: map, 
       icon: SitonImg, 
      }); 

      google.maps.event.addListener(marker, 'click', function() { 
       infowindow.open(map,marker); 
      }); 

      var marker = new google.maps.Marker({ 
       position: latingZee, 
       map: map, 
       icon: ZeeImg, 
      }); 

      google.maps.event.addListener(marker, 'click', function() { 
       infowindow.open(map,marker); 
      }); 

      var marker = new google.maps.Marker({ 
       position: latingHylkema, 
       map: map, 
       icon: HylkemaImg, 
      }); 

      google.maps.event.addListener(marker, 'click', function() { 
       infowindow.open(map,marker); 
      }); 

      var marker = new google.maps.Marker({ 
       position: latingDijkstra, 
       map: map, 
       icon: DijkstraImg, 
      }); 

      google.maps.event.addListener(marker, 'click', function() { 
       infowindow.open(map,marker); 
      }); 
     } 
    </script> 

非常感謝!

+0

更改標記的變量名你同名 – Pratik

+0

如果我定義標記更改該變量並更改標記,然後只有1個標記顯示沒有infowindow。 – Augus

回答

4

另一方面,不是PRATIK的建議,試試這個:

var marker = new google.maps.Marker({ 
    position: latingSiton, 
    map: map, 
    icon: SitonImg 
}); 

// repeat this line after each new Marker is created: 
bindInfoWindow(marker, map, infowindow); 

然後創建一個新的功能:

function bindInfoWindow(marker, map, infowindow) { 
    marker.addListener('click', function() { 
     infowindow.open(map, marker); 
    }); 
} 
+0

啊,這很好,謝謝你們兩位! – Augus