gpt4 book ai didi

typescript - 将传单标记扩展名转换为 Typescript

转载 作者:行者123 更新时间:2023-12-04 08:13:05 24 4
gpt4 key购买 nike

我发现了一个传单扩展,如果 map 标记在当前 View map 范围之外,它会隐藏它们。

import L from 'leaflet';

L.Marker.MyMarker= L.Marker.extend({}).addInitHook(function (this: ILazyMarker) {
this.on(
'add',
function (this: ILazyMarker) {
this._updateIconVisibility = function (this: ILazyMarker) {
var map = this._map,
isVisible = map.getBounds().contains(this.getLatLng()),
wasVisible = this._wasVisible,
icon = this._icon,
iconParent = this._iconParent,
shadow = this._shadow,
shadowParent = this._shadowParent;

// remember parent of icon
if (!iconParent) {
iconParent = this._iconParent = icon.parentNode;
}
if (shadow && !shadowParent) {
shadowParent = this._shadowParent = shadow.parentNode;
}

// add/remove from DOM on change
if (iconParent != null && isVisible != wasVisible) {
if (isVisible) {
iconParent.appendChild(icon);
if (shadowParent != null && shadow) {
shadowParent.appendChild(shadow);
}
} else {
iconParent.removeChild(icon);
if (shadowParent != null && shadow) {
shadowParent.removeChild(shadow);
}
}

this._wasVisible = isVisible;
}
};

// on map size change, remove/add icon from/to DOM
this._map.on('resize moveend zoomend', this._updateIconVisibility, this);
this._updateIconVisibility();
},
this,
);
});
这很好用,但我宁愿在我的项目中使用 Typescript 类来保持一致性。这是我尝试转换它:
import L from 'leaflet';

export default class MyMarker extends L.Marker {
_wasVisible!: boolean;
_icon!: any;
_shadowParent!: any;
_iconParent!: any;

constructor(latlng: L.LatLngExpression, options?: L.MarkerOptions) {
super(latlng, options);
}

onAdd(): this {
this._map.on('resize moveend zoomend', this._updateIconVisibility, this);
this._updateIconVisibility();
return this;
}

_updateIconVisibility() {
var map = this._map,
isVisible = map.getBounds().contains(this.getLatLng()),
wasVisible = this._wasVisible,
icon = this._icon,
iconParent = this._iconParent,
shadow = this._shadow,
shadowParent = this._shadowParent;

// remember parent of icon
if (!iconParent) {
iconParent = this._iconParent = icon.parentNode;
}
if (shadow && !shadowParent) {
shadowParent = this._shadowParent = shadow.parentNode;
}

// add/remove from DOM on change
if (iconParent != null && isVisible != wasVisible) {
if (isVisible) {
iconParent.appendChild(icon);
if (shadowParent != null && shadow) {
shadowParent.appendChild(shadow);
}
} else {
iconParent.removeChild(icon);
if (shadowParent != null && shadow) {
shadowParent.removeChild(shadow);
}
}

this._wasVisible = isVisible;
}
}
}
当我尝试使用基于类的标记时,我添加的每个标记都会出现此错误,并且 map 不会呈现:
TypeError: Cannot read property 'parentNode' of undefined
at MyMarker._updateIconVisibility (leaflet-my-marker.ts:78)
at MyMarker.onAdd (leaflet-my-marker.ts:63)
at MyMarker._layerAdd (leaflet-src.js:6567)
at NewClass.whenReady (leaflet-src.js:4428)
at NewClass.addLayer (leaflet-src.js:6629)
at NewClass.addLayer (leaflet-src.js:6780)
at VueComponent.addLayer (LLayerGroup.js:164)
at VueComponent.FirecamMarker.mounted (MyMarker.vue?6277:99)
at invokeWithErrorHandling (vue.runtime.esm.js:1854)
at callHook (vue.runtime.esm.js:4219)

最佳答案

提议的“StackOverflow 法则”:无论你在一段代码上工作了多长时间,直到你将它发布到 StackOverflow 上,你都无法解决问题
我已经断断续续地工作了好几天......当然,我可以在发布我的问题后立即解决它。我需要调用superonAdd我的 onAdd 中的方法方法,像这样:

onAdd(): this {
super.onAdd(this._map);
this._map.on('resize moveend zoomend', this._updateIconVisibility, this);
this._updateIconVisibility();
return this;
}
这是有道理的,因为原始代码调用了 .addInitHook()不是 .setInitHook() .

关于typescript - 将传单标记扩展名转换为 Typescript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65852126/

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