0

我已经为此苦恼了一段时间,所以我终于崩溃了,决定征求意见。

我必须构造一个使用坐标数组来勾勒地图的对象。我创建了一个数组数组(至少我认为),因此我可以遍历长度并创建所需的任意数量的对象。问题是,我的数组中的值是字符串——它们实际上并没有引用数组。然后该对象变得疯狂并说它是一个无效参数。

我尝试使用 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);
};
4

4 回答 4

3

boundariesAr.push('boundaries' + i)正在推动一个字符串连接以及你的循环中的"boundaries"任何i内容(你最终会得到 50 个字符串:"boundaries0"through "boundaries49")。您应该更改它以推送Array您想要推送的实际对象。哦,确保将迭代变量声明为非全局变量(通过使用var),i这样它就不是全局变量——这将非常糟糕。

//                         V---------------here-------------
for(var i = 0; i < arrays; i++){   //                       |
    boundariesAr.push(boundaries); //                       |
    //i++; /*no need for this, just put it in the for loop - */
};

在这之后,你似乎想split()在你的阵列上运行,这会让你感到悲伤。split()用于字符串,因此您可以将字符串拆分为一个数组,该数组由您传入的任何字符分隔split()。例如,"Hello I am dog".split(" ")将创建数组["Hello", "I", "am", "dog"]. 但是您已经有一个数组,因此除非您需要对该数组进行字符串操作,否则最好删除该行。(现在,无论如何,数组似乎都是空的)。

另一个问题:您设置serviceArea为一个字符串,然后立即用serviceArea = new google.maps.Polygon({...}). 如果您希望它是一个多边形,则不应有理由使用var serviceArea = "serviceArea" + j;. 这会很好:var serviceArea = new google.maps.Polygon({...})

于 2013-10-23T17:25:08.637 回答
2
boundariesAr.push('boundaries' + i);

那就是将一个元素(字符串)推入数组中,而不是在第一个数组中放置另一个数组(正如我假设你希望的那样)。

举一个 10x10 数组的简单示例:

var ary = []; // [] == new Array

for (var i = 0; i < 10; i++){
  ary[i] = []; // add a nested array (2D)
  for (var j = 0; j < 10; j++){
    // add a string within the nested array as an element
    ary[i].push('row' + i + ', col' + j);
  }
}

// debugging -- http://jsfiddle.net/Gv6kP/
console.log(JSON.stringify(ary));
于 2013-10-23T17:27:03.147 回答
2

Your 'updated code' has this: boundariesAr.push(boundaries); but as far as I can tell, boundaries is undefined?

If you want an array of arrays shouldn't it be something like:

var boundaries = [
  [
    new google.maps.LatLng(65.307997,-146.130689),
    new google.maps.LatLng(65.291840,-146.198712)
  ],
  [
    new google.maps.LatLng(64.703884,-147.150958),
    new google.maps.LatLng(64.703189,-147.155442)
  ]
];

You can also create it in this fashion (if it's easier for you to set them by index):

var boundaries = [];
boundaries[0] = [
  'map thing',
  'map thing 2'
]
boundaries[1] = [
  'more map things',
  'more map things 2',
]

This will make it so that when you do boundaries[0] you get

[
  new google.maps.LatLng(65.307997,-146.130689),
  new google.maps.LatLng(65.291840,-146.198712)
]

Which of course enables you the ability to do boundaries[0][0].

Using new Array to make a new array seems silly. Just use var myThing = [];

After you have an actual multi-dimensional array, you should be able to do this:

for(var j=0;j<boundaries.length;j++){
  for( var b=0; b<boundaries[j].length;b++){
    currentItem = boundaries[j][b];
    // At this point currentItem is `new google.maps.LatLng(65.307997,-146.130689)`
    var serviceArea = new google.maps.Polygon({
      path:currentItem,
      geodesic:true,
      strokeColor:'#000',
      strokeOpacity:1.0,
      strokeWeight:2,
      fillColor: '#FF0000',
      fillOpacity: 0.35
    });
  }
  serviceArea.setMap(map); // Where does `map` come from?
}

If your boundaries are actually a whole bunch of separate arrays boundaries1, boundaries2...boundaries50 then you either need to change them to suck less (put them into an array as I've mentioned here), or you're not going to be able to do what you want. Javascript has no way that I know of (And I've looked thoroughly) to retrieve the value from a variable when you only have the name of the variable as a string.

Basically, you can't dynamically call variables the way you're trying to with "boundaries" + i; or anything similar, there just isn't a way to do it unless you put them into a proper array or object data structure.

于 2013-10-23T19:49:23.610 回答
0

这条线是你的问题:

boundariesAr.push('boundaries' + i);

您正在将字符串推送到数组上。要推送数组,只需执行以下操作:

boundariesAr.push([]);

[]数组文字的is 表示法。

于 2013-10-23T17:24:45.107 回答