首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单击Listeners in Loop - Array和Closure

单击Listeners in Loop - Array和Closure
EN

Stack Overflow用户
提问于 2012-12-17 09:05:09
回答 1查看 854关注 0票数 0

我意识到我正在如履薄冰地打开另一个关闭问题,但我已经搜索了,但没有找到我的问题的答案。我有一个Google Maps API v3页面,它从一个代码块生成两个地图-一个以用户当前位置为中心的小地图和一个显示整个区域的较大地图,其中用户的位置被标记为中心或非中心。地图的顶部是一个由14个矩形组成的矩形层。为了生成这两个映射,我必须将矩形放入一个二维数组中,rectangles1表示'map',rectangles2表示'map2':

代码语言:javascript
复制
var rectangles = [0,1,2,3,4,5,6,7,8,9,10,11,12,13];

rectangles[1][0]=new google.maps.Rectangle({
bounds:new google.maps.LatLngBounds(new google.maps.LatLng(a, b), new google.maps.LatLng(x, y)),
map:map,
fillColor:'red',
fillOpacity: 0.3,
strokeOpacity: 0,
url: 'http://example.com',
clickable: true
});

rectangles[2][0]=new google.maps.Rectangle({
bounds:new google.maps.LatLngBounds(new google.maps.LatLng(a, b), new google.maps.LatLng(x, y)),
map:map2,
fillColor:'red',
fillOpacity: 0.3,
strokeOpacity: 0,
url: 'http://example.com',
clickable: true
});

...and等等。这一切都很好,两个地图显示和地理位置工作。现在,我想为每个矩形添加一个单击侦听器,但我不确定引用该数组的是谁。这就是我现在所拥有的:

代码语言:javascript
复制
for ( i = 0; i < rectangles[1].length; i++ ){
google.maps.event.addListener(rectangles[1][i], 'click', function() {
window.location.href = this.url;
});
}
for ( x = 0; x < rectangles[2].length; x++ ){
google.maps.event.addListener(rectangles[2][x], 'click', function() {
window.location.href = this.url;
});
}

这显然是行不通的。如果有人能为我指明正确的方向,我将不胜感激--如果这只是一种显而易见的老生常谈,我深表歉意。总是有一个新的学习者在努力追赶。

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-19 21:07:13

代码语言:javascript
复制
//First, set up `rectangles` as an array containing two arrays.
var rectangles = [];
rectangles[0] = [];
rectangles[1] = [];

//As `google.maps.Rectangle` doesn't accept a `url` option, 
//its url needs to be defined separately from the rectangle itself,
//but in such a way that the two are associated with each other.
//For this we can use a javascript plain object. 
rectangles[0][0] = {
    rect: new google.maps.Rectangle({
        bounds: new google.maps.LatLngBounds(new google.maps.LatLng(a, b), new google.maps.LatLng(x, y)),
        map: map,
        fillColor: 'red',
        fillOpacity: 0.3,
        strokeOpacity: 0,
        clickable: true
    }),
    url: 'http://example.com'
};
rectangles[1][0] = new google.maps.Rectangle({
    ...
});
rectangles[0][1] = new google.maps.Rectangle({
    ...
});
rectangles[1][1] = new google.maps.Rectangle({
    ...
});
rectangles[0][2] = new google.maps.Rectangle({
    ...
});
rectangles[1][2] = new google.maps.Rectangle({
    ...
});

//Now to attach the click listeners.

//First we define a function that adds a click listener.
//By doing this in its own function, a closure is formed,
//trapping the formal variable `rectObj` and making `rectObj.url` 
//accessible to the listener when it is called in response to future clicks.
function addClickListener(rectObj) {
    google.maps.event.addListener(rectObj.rect, 'click', function() {
        window.location.href = rectObj.url;
    });
}

//Now, we can loop through the `rectangles` arrays, adding listeners.
for ( i = 0; i < 2; i++ ) {
    for ( j = 0; j < 14; j++ ) {
        if(rectangles[i][j]) {//safety
            addClickListener(rectangles[i][j]);
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13907103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档