2017-07-07 150 views
0

我想創建一個信息窗口打開與用戶點擊標記的地圖。到目前爲止,代碼似乎沒有工作,地圖之前顯示,但一旦我把信息窗口彈出窗口的代碼不僅顯示信息窗口,但地圖也停止顯示在我的網頁上。下面的代碼:谷歌地圖API與infowindow不工作

<div id= "map"> </div> 


     <script> 
     var map; 

      var markers=[]; 

     function initMap(){ 

      map= new google.maps.Map(document.getElementById('map'),{ 

       center:{ lat:40.7413549, lng:-73.9980244}, 
       zoom:13 
      }); 


     var locations=[ 


      {title:"Park Ave Penthouse",location:{lat:40.7713024,lng:-73.9632393}}, 
      {title:"Chelsea Loft", location;{lat:40.7444883,lng:-73.9949465}}, 
      {title:"Union Square Open Floor Plan",location:{lat:40.7347062,lng:-73.984377}}, 
      {title:"East Village Hip Studio",location:{lat:40.7281777,lng:-73.984377}}, 
      {title:"TriBeCa Artsy Bachelor Pad",location:{lat:40.7195264,lng:-74.0089934}},  
      {title:"Chinatown Homey Space",location:{lat:40.7180628,lng:-73.9961237}} 

     ]; 
      var largeInfowindow= new google.maps.InfoWindow(); 
      var bounds= new google.maps.LatLngBounds(); 


      for (var i=0; i< locations.lenght;i++){ 

       var position = locations[i].location; 
       var title = locations[i].title; 

       var marker= new google.maps.Marker({ 

        map: map, 
        position:position, 
        title:title, 
        animation:google.maps.Animation.Drop, 
        id:i 

        }); 

        markers.push(marker); 
        bounds.extend(marker.position); 
        marker.addListner ("click",function(){ 

        populateInfoWindow(this,largeInfowindow); 




      }); 

      map.fitBounds(bounds); 






       }; 

       markers.push(marker); 

       markers.addListner("click",function(){ 

        populateInfoWindow(this,largeInfoWindow); 
       }); 


       } 



      function populateInfoWindow (marker, infowindow){ 


       if(infowindow.marker=!marker){ 

        infowindow.marker= marker; 
        infowindow.setContent("<div>"+marker.title+"</div>"); 

        infowindow.open(map,marker); 

        infowindow.addListner("closeclick",function(){ 


         infowindow.setMarker(null); 
        }); 






       } 

      } 


<script async defer 
       src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAvrWT4KKcsDSSGYXpxRry3m7j90pPjZgA&v=3&callback=initMap"></script> 
+0

請檢查控制檯的腳本錯誤。 –

+0

我得到一個JavaScript錯誤與張貼的代碼:'未捕獲的SyntaxError:意外的標記;'在這個'標題:「切爾西閣樓」, 位置; { 緯度:40.7444883, LNG:-73.9949465 }' – geocodezip

+0

然後'遺漏的類型錯誤:markers.addListner不是function' – geocodezip

回答

1

的錯誤是在這一行:

if(infowindow.marker=!marker){ //it is "!=" and not "=!" 

始終爲JavaScript錯誤檢查控制檯。 infowindow代碼之後,你甚至無法看到這意味着你有JavaScript錯誤的毛。