将数组转换为谷歌Lat / Lng数组?

时间:2012-05-16 13:28:15

标签: javascript google-maps

我有阵列

[[[11.11,11.11],[22.22,22.22],[33.33,33.33]],[[11.11,11.11],[22.22,22.22],[33.33,33.33]]]

我想做的是将其转化为,

  var paths = [[
    new google.maps.LatLng(38.872886, -77.054720),
    new google.maps.LatLng(38.872602, -77.058046),
    new google.maps.LatLng(38.870080, -77.058604),
    new google.maps.LatLng(38.868894, -77.055664),
    new google.maps.LatLng(38.870598, -77.053346)
  ], [
    new google.maps.LatLng(38.871684, -77.056780),
    new google.maps.LatLng(38.871867, -77.055449),
    new google.maps.LatLng(38.870915, -77.054891),
    new google.maps.LatLng(38.870113, -77.055836),
    new google.maps.LatLng(38.870581, -77.057037)
  ]];

每个

[[11.11,11.11],[22.22,22.22],[33.33,33.33]]

我的阵列是一个新的阵列Google LatLng Markers。

我已尝试在数组上使用map函数,如下所示:

  point.map(new google.maps.LatLng(point))

但是收到NaN错误。我是否需要打破每一个点,或者我可以通过诸如map之类的调用更流畅地执行此操作?

1 个答案:

答案 0 :(得分:1)

嵌套循环?

var input = [
    [
        [11.11, 11.11],
        [22.22, 22.22],
        [33.33, 33.33]
    ],
    [
        [11.11, 11.11],
        [22.22, 22.22],
        [33.33, 33.33]
    ]
];
var paths = [];
for (var i = 0; i < input.length; i++) {
    var path = [];
    for (var j = 0; j < input[i].length; j++) {
        path.push(new google.maps.LatLng(input[i][j][0], input[i][j][1]));
    }
    paths.push(path);
}
console.dir(paths);​

Demo here