0

我意识到我正在如履薄冰地打开另一个关闭问题,但我已经搜索并找不到我的问题的答案。我有一个 Google Maps API v3 页面,它从一个代码块生成两张地图 - 一张以用户当前位置为中心的小地图和一张更大的地图,显示整个区域,用户的位置标记在哪里,中心与否。地图顶部是一个由 14 个矩形组成的矩形层。为了生成这两个地图,我必须将矩形放在二维数组中,矩形 [1] 用于“地图”,矩形 [2] 用于“地图2”:

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

...等等。一切正常,显示两张地图并且地理位置有效。现在我想为每个矩形添加一个点击监听器,但我不确定谁来引用这个数组。这就是我现在所拥有的:

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

这显然行不通。我已经看到了关闭问题的各种解决方案,但我不确定在引用两个矩形数组时我是否朝着正确的方向前进 - 或者我什至需要定义两组不同的点击侦听器。如果有人能指出我正确的方向,我将非常感激 - 如果这只是越过看起来很明显的旧领域,我很抱歉。总是有一个新的学习者正在努力追赶。

谢谢。

4

1 回答 1

0
//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]);
        }
    }
}
于 2012-12-19T13:07:13.043 回答