每个标记的多标记和信息框

时间:2015-03-31 19:42:25

标签: javascript google-maps google-maps-api-3

我尝试制作自定义infobox。如果我运行一个标记,它可以工作,但如果我尝试imgUr(数组有每个标记的坐标,名称和其他属性)有多个标记,我无法做到工作。它只显示第一个标记,没有其他标记,没有infobox

以下是代码:

for (i = 0; i < imgAr.length; i++) {

    var location = new google.maps.LatLng(imgAr[i].coords.coordinates[1],
                                          imgAr[i].coords.coordinates[0]);

    var marker = new google.maps.Marker({
                        position : location,
                        map : map,
                        icon : "icons/coffee1.png" ,
                        animation : google.maps.Animation.DROP,
                        infoboxIndex : i  // <--- That's the extra attribute
    });

    var content = "<h5> Name:" + imgAr[i].name +
                  "<br> Description:" + imgAr[i].description +
                  "<br><img src='" + imgAr[i].image +
                  "' width='100px' height='100px'/> <h5>";

    var var_infobox_props = {
        content: content,
        disableAutoPan: false,
        maxWidth: 0,
        pixelOffset: new google.maps.Size(-10, 0),
        zIndex: null,
        boxClass: "myInfobox",
        closeBoxMargin: "2px",
        closeBoxURL: "icons/close.png",
        infoBoxClearance: new google.maps.Size(1, 1),
        visible: true,
        pane: "floatPane",
        enableEventPropagation: false
    };

    var var_infobox = new InfoBox(var_infobox_props);

    google.maps.event.addListener(marker, 'mouseover',
        function(event) {
            map.panTo(event.latLng);
            map.setZoom(16);
            infocoffee[this.infoboxIndex].open(map, this);
        }
    );

    // Save infobox in array
    infocoffee.push(var_infobox);

    // Save marker in array
    markers.push(marker);
}

markers.setMap(map);

什么不起作用?我该如何解决?

1 个答案:

答案 0 :(得分:1)

听起来infocoffee.push(var_infobox);markers.push(marker);会导致错误,javascript会从那里停止执行(这就是为什么你只有一个标记。)

我在JSFiddle上重现它。请查看the working samplethe sample with error

= - = - = - 编辑 - = - = - =
同样的概念应该适用于信息框插件,因为它是shown here