-1

我有一组显示良好的标记。我遇到的问题是为每个使用数组中保存的内容显示一个信息框。该数组称为 store_pins,元素 4 和 5 保存要传递给 InfoBox 的值(元素 4 包含图像的文件名,5 包含图像的标题)。信息框代码将

单击标记时需要调用 InfoBox,并且应该在我的代码底部有注释 // INFO BOX CODE GOES HERE。

我的完整代码如下:

    var map;
    var pop_up_info = "border: 0px solid black; background-color: #ffffff; padding:15px; margin-top: 8px; border-radius:10px; -moz-border-radius: 10px; -webkit-border-radius: 10px; box-shadow: 1px 1px #888;";


    google.maps.event.addDomListener(window, 'load', initialize);
    
    function initialize() {
        var map_center  = new google.maps.LatLng(55.144178,-2.254122);
        var store_pins  = new Array();
        var pin_marker  = new Array();
        var pin_info    = new Array();
        var pin_infoop  = new Array();
        
        store_pins = [
            ['Bellingham Co-Op', 55.144178, -2.254122,'pin','bellinghamcoop.jpg','Staff at Bellingham Co-Op'],
            ['Leicester Tigers - Kingston Park', 55.018754, -1.672230,'rugby','kingparktigers.jpg','Stu with the Leicester Tigers Rugby Team'],
            ['The Hawick PSA Team', 55.421825, -2.797123,'rugby','hawickpsa.jpg','The Hawick PSA Team']
        ];

    var myOptions = {
        zoom: 3,
        minZoom: 3,
        center: map_center,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    
        map = new google.maps.Map(document.getElementById("trackingT-map"), myOptions);
    
        // Main Loop
        
        
        for(i=0;i<store_pins.length;i++)
        {
        var pos = new google.maps.LatLng(store_pins[i][1], store_pins[i][2]);
        var pinIcon = {
            url: 'icons/shirtpin.png',
            //The size image file.
            size: new google.maps.Size(50, 55),
            //The point on the image to measure the anchor from. 0, 0 is the top left.
            origin: new google.maps.Point(0, 0),
            //The x y coordinates of the anchor point on the marker. e.g. If your map marker was a drawing pin then the anchor would be the tip of the pin.
            anchor: new google.maps.Point(25, 20)
        };
        var pinShape = {
            coord: [0,0,50,55],
            type: 'rect'
        };

        pin_marker[i] = new google.maps.Marker(
        {
                position:       pos,
                map:            map,
                title:          store_pins[i][0],
                icon:           pinIcon,
                shape:          pinShape,
                zIndex:         107
           } 
        );
            
        pin_infoop[i] = {
                disableAutoPan: false,
                maxWidth: 0,
                pixelOffset: new google.maps.Size(-241, 0),
                zIndex: null,
                boxStyle: { 
                background: "url('infobox/pop_up_box_top_arrow.png') no-repeat",
                opacity: 1,
                width: "430px"
                },
                closeBoxMargin: "10px 2px 2px 2px",
                closeBoxURL: "icons/button_close.png",
                infoBoxClearance: new google.maps.Size(1, 1),
                isHidden: false,
                pane: "floatPane",
                enableEventPropagation: false,
                content:   store_pins[i][5]
        };

        pin_info[i] = new InfoBox(pin_infoop[i]);

            
            
        google.maps.event.addListener(pin_marker[i], 'click', function() {
            map.panTo(this.position);
            map.setZoom(10);
            pin_info[i].open(map, this);
        });
} 
};

4

1 回答 1

0

我的代码出现 javascript 错误:

Uncaught TypeError: Cannot read property 'open' of undefined

在这条线上:

pin_info[i].open(map, this);

i=3,pin_info 的最高可用索引为 2。

您的问题是在循环中定义信息窗口/框的常见问题。用匿名函数闭包解决了这个问题:

改变这个:

    google.maps.event.addListener(pin_marker[i], 'click', function() {
        map.panTo(this.position);
        map.setZoom(10);
        pin_info[i].open(map, this);
    });

到:

    google.maps.event.addListener(pin_marker[i], 'click', (function(marker, i) {
    return function() {
        map.panTo(this.position);
        map.setZoom(10);
        pin_info[i].open(map, this);
    }
  })(pin_marker[i], i));

概念证明小提琴(必须更改信息框的图标和一些属性,以便我可以看到发生了什么,因为您的代码不能按原样工作)

于 2015-03-15T10:02:42.090 回答