vue3 项目使用leaflet 的marker pupop,tooltip伸缩地图报错TypeError: Cannot read properties of null (reading '_latLngToNewLayerPoint')
可以去leaflet 的github上查看issue _latLngToNewLayerPoint
问题描述:点击marker点的弹框信息后伸缩,操作几次就会报以上的错,打印出来看到获取不到地图map信息。
解决办法:重写marker pupop,tooltip方法,判断是否有当前的this._map,有的话继续操作,没有则返回return。
const map = L.map("map", {
center: [22.65, 114.1],
zoom: 6,
maxZoom: 18,
minZoom: 3,
attributionControl: false,
zoomControl: false,
// preferCanvas: true,
});
// 创建不同地图图层
const normalLayer = L.tileLayer.chinaProvider("GaoDe.Normal.Map", {
maxZoom: 18,
minZoom: 3,
});
// 重新Tooltip和L.Popup 的扩展方法(这个需要在你定义地图后重写,位置自己看着放)
if (map) {
// 添加 Tooltip 的扩展方法
// L.Tooltip.include({
// _animateZoom(e: any) {
// console.log("Tooltip", this, this._map);
// if (this._map) {
// console.log("添加 Tooltip 的扩展方法 :>> ", this._map);
// const pos = this._map._latLngToNewLayerPoint(
// this._latlng,
// e.zoom,
// e.center
// );
// this._setPosition(pos);
// } else {
// return;
// }
// },
// });
L.Tooltip.prototype._animateZoom = function (e: any) {
if (!this._map) {
return;
}
let pos = this._map._latLngToNewLayerPoint(
this._latlng,
e.zoom,
e.center
);
this._setPosition(pos);
};
L.Tooltip.prototype._updatePosition = function () {
if (!this._map) {
return;
}
let pos = this._map.latLngToLayerPoint(this._latlng);
this._setPosition(pos);
};
// 添加 Popup 的扩展方法
L.Popup.include({
_animateZoom(e: any) {
console.log("Popup", this._map);
if (this._map) {
const pos = this._map._latLngToNewLayerPoint(
this._latlng,
e.zoom,
e.center
);
const anchor = this._getAnchor();
L.DomUtil.setPosition(this._container, pos.add(anchor));
} else {
return;
}
},
});
// 添加 Marker 的扩展方法
// L.Marker.include({
// _animateZoom(opt: any) {
// console.log("Marker", this._map);
// if (this._map) {
// const pos = this._map
// ._latLngToNewLayerPoint(this._latlng, opt.zoom, opt.center)
// .round();
// this._setPos(pos);
// }
// },
// });
}