Error: Uncaught (in promise): Error: Invalid LatLng object: (undefined, undefined)
//coords
points: Array<any> = [];
// short data json
data =[
{
"22a7f183": [
"502F0C",
56.9227,
23.9790,
16,
0,
0,
"0000",
"T-EVRA43",
"",
"",
1573389319,
"RIX",
"",
"",
1,
0,
"GPSMONT",
0,
""
],
}]
MarkerMap() {
this.points = Object.keys(this.data)
.map(key => this.data[key])
.map((position) => ({
lat: position[1],
lng: position[2]
}));
this.points.forEach((point) => {
L.marker([point.lat,point.lng]).addTo(globalMap)
.bindPopup('Ionic 4 <br> Leaflet.')
.openPopup();
console.log(point.lat,point.lng)
});
}
我又如何避免在数组内部出现空对象?
答案 0 :(得分:1)
您的数据源是什么?也许某些记录没有坐标?你确认了吗?
如果数组中的元素不完整,则可以使用filter
方法过滤数组:
this.points = Object.keys(this.data)
.map(key => this.data[key])
.map((position) => ({
lat: position[1],
lng: position[2]
}))
.filter(position => position.lat && position.lng);
另外,您应该检查坐标是数字。