【发布时间】:2021-11-14 13:51:19
【问题描述】:
我有一个带有光标的传单地图,其中显示光标的纬度坐标。
L.CursorHandler = L.Handler.extend({
addHooks: function () {
this._popup = new L.Popup();
this._map.on('mouseover', this._open, this);
this._map.on('mousemove', this._update, this);
this._map.on('mouseout', this._close, this);
},
removeHooks: function () {
this._map.off('mouseover', this._open, this);
this._map.off('mousemove', this._update, this);
this._map.off('mouseout', this._close, this);
},
_open: function (e) {
this._update(e);
this._popup.openOn(this._map);
},
_close: function () {
this._map.closePopup(this._popup);
},
_update: function (e) {
this._popup.setLatLng(e.latlng)
.setContent(e.latlng.toString());
}
});
L.Map.addInitHook('addHandler', 'cursor', L.CursorHandler);
例如,原始代码将 lat-lon 格式化为“LatLng(12.25232563, 43.52432453)”。我编辑了格式,以便在没有 LatLng 或括号的情况下显示两位小数 - 例如:12.25,43.52。
但是,在某些时候,配置变得固定,现在仅以这种后来的 2 位小数格式显示:
这种行为很奇怪,因为即使我将代码更改为类似
_update: function (e) {
this._popup.setLatLng(e.latlng)
.setContent("Hello");
}
地图弹出格式没有改变,仍然显示之前的十进制纬度。我不确定为什么会发生这种情况,以及为什么对代码的更改不会反映在地图上。更奇怪的是,我不确定地图从哪里绘制逻辑,因为即使删除了整个方法,它仍然顽固地继续显示经纬度。停止 React (npm) 服务器/重新加载网络浏览器时,行为不会改变。
这可能是由于地图的某些内部状态需要在页面加载时删除/更新。我会很感激任何建议!
【问题讨论】:
-
请您提供您的问题的重现示例,例如使用 CodeSandbox 或 StackBlitz 吗?虽然只是重新加载服务器可能确实不会改变您当前的页面,但重新加载浏览器应该可以工作。
-
嗨。我无法让它在沙盒中工作,但这是我从中获得它的示例 - 可能会有所帮助:stackoverflow.com/questions/46053309/…
-
我已经弄清楚了。感谢您的建议!
标签: javascript leaflet hook handler react-leaflet