gpt4 book ai didi

leaflet - 错误类型错误 : Cannot read property '_leaflet_pos' of undefined

转载 作者:行者123 更新时间:2023-12-03 08:28:09 26 4
gpt4 key购买 nike

我有使用 ngx-leaflet 和路由的 Angular 10 应用程序。我有一个 map 组件,它根据用户选择在 map 上动态显示自定义标记。我从 map 组件 View 导航到另一个组件。然后我导航回 map 组件。用户可以更改日期,并基于此,旧的标记层将被删除,新的标记层将被加载并显示。一切正常,但我总是收到此错误:

ERROR TypeError: Cannot read property '_leaflet_pos' of undefined
at getPosition (leaflet-src.js:2450)
at NewClass._getMapPanePos (leaflet-src.js:4439)
at NewClass._moved (leaflet-src.js:4443)
at NewClass.getCenter (leaflet-src.js:3798)
at NewClass.setZoom (leaflet-src.js:3181)
at SafeSubscriber._next (map.component.ts:960)
at SafeSubscriber.__tryOrUnsub (Subscriber.js:183)
at SafeSubscriber.next (Subscriber.js:122)
at Subscriber._next (Subscriber.js:72)
at Subscriber.next (Subscriber.js:49)

只有当我返回到 map 组件时,我才能重现此错误。如果我只停留在 map 组件上,则不会显示错误。我已经寻找修复方法,但从我发现的情况来看,似乎没有人真正知道为什么会发生这种情况以及如何修复此错误。我在 GitHub 上发现这两个问题在 Vue.js 中处理相同的问题,所以我猜这是传单本身的问题,而不是 ngx-leaflet 的问题。

https://github.com/vue-leaflet/Vue2Leaflet/issues/613

https://github.com/stefanocudini/leaflet-search/issues/129

我尝试改变这一点:

  function getPosition(el) {
// this method is only used for elements previously positioned using setPosition,
// so it's safe to cache the position for performance

return el._leaflet_pos || new Point(0, 0);

}

对此:

 function getPosition(el) {
// this method is only used for elements previously positioned using setPosition,
// so it's safe to cache the position for performance

if(el){
return el._leaflet_pos || new Point(0, 0);
}
else{
return new Point(0, 0);
}

}

但是错误看起来像这样:

ERROR TypeError: Cannot set property '_leaflet_pos' of undefined
at setPosition (leaflet-src.js:2433)
at NewClass._resetView (leaflet-src.js:4154)
at NewClass.setView (leaflet-src.js:3174)
at NewClass.setZoom (leaflet-src.js:3186)
at SafeSubscriber._next (map.component.ts:960)
at SafeSubscriber.__tryOrUnsub (Subscriber.js:183)
at SafeSubscriber.next (Subscriber.js:122)
at Subscriber._next (Subscriber.js:72)
at Subscriber.next (Subscriber.js:49)
at MapSubscriber._next (map.js:35)

更新:作为@pk。在评论中建议,当我在删除旧标记之前不调用 setZoom 或调用它时,我会收到此错误:

ERROR TypeError: Cannot read property 'appendChild' of undefined
at NewClass._initIcon (leaflet-src.js:7608)
at NewClass._initIcon (leaflet.rotatedMarker.js:23)
at NewClass.onAdd (leaflet-src.js:7460)
at NewClass._layerAdd (leaflet-src.js:6572)
at NewClass.whenReady (leaflet-src.js:4433)
at NewClass.addLayer (leaflet-src.js:6634)
at NewClass.eachLayer (leaflet-src.js:6861)
at NewClass.onAdd (leaflet-src.js:6845)
at NewClass._layerAdd (leaflet-src.js:6572)
at NewClass.whenReady (leaflet-src.js:4433)

更新2:当我不向 map 添加新的标记图层(this.deskLayer.addTo(map))时,错误消失,但我想向 map 添加新的标记...这是用户更改日期时发生的情况:

onMapReady(map: Map) {

//listening for USER DATE CHANGE
this.userFloorService.sharedUserSelectedDate
.pipe(skip(1))
.subscribe(() => {
this.deskLayer.remove(); // first remove old desks
this.userFloorService // then get desks and reservations
.getFloor(this.floorNumber)
.subscribe( (data) => {
// create new reservations
let reservationsArr = data.records[0].reservations;
// create new DESKS
let deskMarkers = [];
data.records[0].desks.forEach((desk) => {
let deskId = desk.desk_id;
let deskMarker = marker(
[
desk.desk_coordinate.coordinates[0],
desk.desk_coordinate.coordinates[1],
],
{
title: this.getDeskTitle(desk, reservationsArr), // set desk title to RESERVED/FREE
rotationAngle: desk.desk_angle,
rotationOrigin: 'center center',
riseOnHover: true
}
).on('click', () => {
this.zone.run(() => {
this.openDeskDialog(deskMarker.options.title,deskId);
});
});
deskMarker.setIcon(this.getDeskIcon(deskMarker)); // for displaying desk icons on zoomLvl -1.5
deskMarkers.push(deskMarker);
});
this.deskLayer = layerGroup(deskMarkers); // add new desks to deskLayer
this.layersControl.overlays['Desks'] = this.deskLayer; // reassign desks in overlays for correct desk layer toggling

this.deskLayer.addTo(map);
map.setZoom(-1); // set zoom
},
error =>{
console.log(error);
}
);
});

最佳答案

事实证明,这些错误的发生是因为我使用BehaviorSubject在组件之间传递数据,并且每次我导航出并返回到 map 组件时,都会创建对BehaviourSubject的新订阅,而不会破坏旧的订阅。因此,每次我从 map 组件导航时销毁订阅就解决了这个问题。也许这会对某人有所帮助。

关于leaflet - 错误类型错误 : Cannot read property '_leaflet_pos' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66009013/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com