我已经为此苦恼了一段时间,所以我终于崩溃了,决定征求意见。
我必须构造一个使用坐标数组来勾勒地图的对象。我创建了一个数组数组(至少我认为),因此我可以遍历长度并创建所需的任意数量的对象。问题是,我的数组中的值是字符串——它们实际上并没有引用数组。然后该对象变得疯狂并说它是一个无效参数。
我尝试使用 split(),但似乎没有效果。我手动输入了路径:boundaries0,这似乎工作得很好。我已经看这个很久了,我想我已经失去了脑细胞。如果有人甚至可以让我走上正确的道路,那就太棒了。
var arrays = 50; //Set this to how many arrays are created.
var boundariesAr = new Array;
var boundaries = new Array;
for(i=0;i<arrays;){
boundariesAr.push('boundaries' + i);
i++;
};
for(j=0;j<boundariesAr.length;){
var serviceArea = "serviceArea" + j;
var currentArray = boundariesAr[j];
var currentItem = currentArray.split(" ");
serviceArea = new google.maps.Polygon({
path:currentItem,
geodesic:true,
strokeColor:'#000',
strokeOpacity:1.0,
strokeWeight:2,
fillColor: '#FF0000',
fillOpacity: 0.35
});
serviceArea.setMap(map);
j++;
};
编辑 - 更新的代码(只有一个片段)这是对数组外观的截断视图:
var boundaries0 = [
new google.maps.LatLng(65.307997,-146.130689),
new google.maps.LatLng(65.291840,-146.198712),
];
var boundaries1 = [
new google.maps.LatLng(64.703884,-147.150958),
new google.maps.LatLng(64.703189,-147.155442),
];
var arrays = 50; //boundaries0[],boundaries1[], etc
var boundariesAr = new Array;
for(var i=0;i<arrays;i++){
boundariesAr.push(boundaries);
};
for(var j=0;j<boundariesAr.length;j++){
var serviceArea = "serviceArea" + j;
var currentArray = boundariesAr[j];
var currentItem = currentArray.split(" ");
serviceArea = new google.maps.Polygon({
path:currentItem,
geodesic:true,
strokeColor:'#000',
strokeOpacity:1.0,
strokeWeight:2,
fillColor: '#FF0000',
fillOpacity: 0.35
});
serviceArea.setMap(map);
};
感谢一些出色的人,他们抽出时间来提供帮助。如果可以的话,我会给你买饮料。
这终于奏效了——(我觉得一开始就没有嵌套数组很傻)
var boundaries = [];
boundaries[0] = [
new google.maps.LatLng(65.307997,-146.130689),
new google.maps.LatLng(65.291840,-146.198712),
];
boundaries[1] = [
new google.maps.LatLng(64.703884,-147.150958),
new google.maps.LatLng(64.703189,-147.155442),
];
var arrays = 50; //Set this to how many arrays are created.
for(var j=0;j<arrays;j++){
serviceArea = new google.maps.Polygon({
path:boundaries[j],
geodesic:true,
strokeColor:'#000',
strokeOpacity:1.0,
strokeWeight:2,
fillColor: '#FF0000',
fillOpacity: 0.35
});
serviceArea.setMap(map);
};