如何将数组的第4个元素显示为信息窗口的内容?

时间:2014-03-04 23:09:48

标签: javascript arrays infowindow markers

大家好我正为每个标记创建一个信息窗口,然后我将内容传递给变量,以便将数组的第4个元素作为内容分配,但似乎它不起作用。知道如何在每个信息窗口中显示数组的第4个元素吗?非常感谢。

var infowindow = null;
function initialize() {
var latlng = new google.maps.LatLng(52.474, -1.868);

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

var map = new google.maps.Map(document.getElementById("map"), myOptions);
var image = 'marker1.png';

var countries = [
['england', 51.508515 , -0.125487, 5,'this is england'],
['france', 46.227638 , 2.213749, 4,'this is france'],
['switcherland', 46.818188 , 8.227512, 3,'this is switcherland'],
['italy', 41.871940 , 12.56738, 2,'this is italy'],
['greece', 39.074208 , 21.824312, 1,'this is greece']
];


 setMarkers(map, countries);
    infowindow = new google.maps.InfoWindow({

    content: 'hi'
        });


function setMarkers(map, markers) {

    for (var i = 0; i < markers.length; i++) {
        var countries = markers[i];
        var siteLatLng = new google.maps.LatLng(countries[1], countries[2]);
        var marker = new google.maps.Marker({
            position: siteLatLng,
            map: map,
            title: countries[0],
            zIndex: countries[6],
            html: countries[6]

        });



        google.maps.event.addListener(marker, "click", function () {


            infowindow.open(map,this);
        });
    }
 }
 }

google.maps.event.addDomListener(window, 'load', initialize);

1 个答案:

答案 0 :(得分:1)

var countries

是一个数组数组。 索引4 实际上指向数组的第5个元素(数组索引从0开始),所以使用

var contentString = countries[4] 

你得到数组,而不是字符串!

['greece', 39.074208 , 21.824312, 1,'this is greece']

看起来你想要这个数组的第5个元素(索引4)

contentString[4]

'这是希腊'