An array of infoWindow in Google maps api

后端 未结 2 753
逝去的感伤
逝去的感伤 2021-01-26 08:14

I looked for error and I couldn\'t find it. Any google maps infoWindow always shows the same information for some reason.

Here is a coffeescript code

inf         


        
2条回答
  •  说谎
    说谎 (楼主)
    2021-01-26 08:51

    you need to obtain closure on the variable company. You can do that by creating your markers in a separate function. for example:

    infowindow = new google.maps.InfoWindow()
    for(var n = 0 ; n < n companiesData.length ;n++){
      createMarker(companiesData[n]);
    }
    
    function createMarker(data){
      var marker = new google.maps.Marker({
        position: new google.maps.LatLng(parseFloat(data.latitude), parseFloat(data.longitude)),
        map: map
      })
    
      #debugger ---> each company.name is different!
      google.maps.event.addListener(marker, 'click', function(){
        infowindow.setContent(data.name);
        infowindow.open(map,this);
      }
      )
    }
    

    Explanation here.

提交回复
热议问题