【发布时间】:2018-12-02 15:18:16
【问题描述】:
我在尝试创建 google.maps.LatLng 数组时遇到以下错误:
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'NaN' of undefined
这就是我正在做的事情:
我正在动态填充 GPS 坐标的二维数组,如下所示:
const array = [
[1.23123, -1.23123],
[4.56456, -4.56456],
[7.89789, -7.89789]
]
我正在尝试通过执行以下操作将其转换为 google.maps.LatLng 数组:
const latLngArray = [];
for (let i = 0; i < array.length; i++) {
latLngArray.push(new google.maps.LatLng(array[i][0], array[i][1]));
}
然后像这样将它分配给我的新 HeatmapLayer:
this.heatmap = new google.maps.visualization.HeatmapLayer({
data: latLngArray,
map: this.map
});
地图加载得很好,但与坐标相关的点却没有(我遇到了那个错误)。
但是,如果我手动创建以下函数并将其分配给 HeatmapLayer 中的数据字段,它就可以工作:
getPoints() {
return [
new google.maps.LatLng(1.23123, -1.23123),
new google.maps.LatLng(4.56456, -4.56456),
new google.maps.LatLng(7.89789, -7.89789)
]
}
我做错了什么?
【问题讨论】:
-
你忘了
push(),即它应该是latLngArray.push(...) -
对不起,我抄错了。我在我的代码中推送它(我将编辑问题)
-
数组是由任何字符串值还是仅由数字组成?
-
发布的代码对我有用 (fiddle)。请提供一个 minimal reproducible example 来证明您的问题。
-
原来问题出在其他地方。当前代码运行正常。
标签: javascript arrays angular google-maps-api-3 heatmap