【问题标题】:Array of Markers - Passing info held in array to InfoBox标记数组 - 将数组中保存的信息传递给 InfoBox
【发布时间】:2015-05-17 06:17:24
【问题描述】:

我有一组显示良好的标记。我遇到的问题是为每个使用数组中保存的内容显示一个信息框。该数组称为 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);
        });
} 
};

【问题讨论】:

  • 您的问题中没有任何信息框代码,您尝试过哪些您认为应该可以的方法?
  • 对不起,贴错代码sn-p。我在上面编辑过。 pin_infoop[i] 是一个包含 infoBox 选项的数组。 pin_info[i] 是一个包含 infoBox 本身的数组。我在 addListener 事件中收到以下错误:Cannot read property 'open' of undefined 发生在语句 pin_info[i].open(map, this);很明显,将 InfoBox 数组传递给 Listener 存在问题。希望你能帮忙。

标签: arrays api google-maps markers infobox


【解决方案1】:

您的代码出现 javascript 错误:

Uncaught TypeError: Cannot read property 'open' of undefined

在这一行:

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

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

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

改变这个:

    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));

proof of concept fiddle(必须更改信息框的图标和一些属性,这样我才能看到发生了什么,因为您的代码无法按原样运行)

【讨论】:

  • 我可以吻你,但我妻子不喜欢!非常感谢,多年来我一直在为此挠头。只是一个简单的问题,你有: google.maps.event.addListener(pin_marker[i], 'click', (function(marker, i) { 它不应该说 (function(pin_marker, i) { 吗?跨度>
  • 这是在proof of concept fiddle 中工作的代码。标记变量实际上并未在该代码中使用(我从示例中复制了它,您的代码使用marker 将使用this),i 上的闭包很重要。
猜你喜欢
  • 2023-03-16
  • 2021-10-22
  • 1970-01-01
  • 2015-02-25
  • 1970-01-01
  • 2020-08-21
  • 2020-09-10
  • 1970-01-01
  • 2012-05-29
相关资源
最近更新 更多