2012-08-23 57 views
0

我查找錯誤,但無法找到它。任何谷歌地圖infoWindow總是出於某種原因顯示相同的信息。Google maps api中的infoWindow數組

這裏是一個CoffeeScript的代碼

infowindow = new google.maps.InfoWindow() 
for company in companiesData 
    marker = new google.maps.Marker({ 
    position: new google.maps.LatLng(company.latitude, company.longitude) 
    map: map 
    }) 

    #debugger ---> each company.name is different! 
    google.maps.event.addListener(marker, 'click',()-> 
    infowindow.setContent(company.name) 
    infowindow.open(map,this) 
) 

我調試它,看到每個company.name是不同的。

輸出的JavaScript

infowindow = new google.maps.InfoWindow(); 
    _results = []; 
    for (_j = 0, _len1 = companiesData.length; _j < _len1; _j++) { 
    company = companiesData[_j]; 
    marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(company.latitude, company.longitude), 
     map: map 
    }); 
    _results.push(google.maps.event.addListener(marker, 'click', function() { 
     infowindow.setContent(company.name); 
     return infowindow.open(map, this); 
    })); 
    } 
    return _results; 
}); 

那麼,是一個錯誤?

回答

3

您需要獲得可變公司的關閉。您可以通過在獨立功能中創建標記來實現這一點。例如:

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.

+0

沒錯。但爲什麼它工作?有什麼不同? – Alexandre

+0

查看我在回覆的底部發布的鏈接,其中顯示「此處解釋」。 – Marcelo