reactjs - DeckGL & Mapbox:如何在地图移动时访问 ref 元素?
问题描述
console.log
每次我在地图上移动时,我都会尝试在我的 React 应用程序中找到 Mapbox 地图的边界。
这是我目前的尝试:
return (
<>
<DeckGL
layers={layers}
initialViewState={INITIAL_VIEW_STATE}
controller={true}
onViewStateChange={stateChangeFunc}
>
<ReactMapGL
reuseMaps
mapStyle={mapStyle}
preventStyleDiffing={true}
ref={(ref) => console.log(ref.getMap().getBounds())}
mapboxApiAccessToken={token}
/>
</DeckGL>
</>
);
地图加载时会打印边界,但在地图上移动时打印时遇到问题。prop
我应该使用它来访问此功能吗?装ref={(ref) => console.log(ref.getMap().getBounds())}
进去DeckGL
是不行的。是否有相当于onViewStateChange
for的道具ReactMapGL
?这可能允许我创建一个打印出ref
.
解决方案
你可以试试:
- 直接从甲板上收听
onViewStateChange
(建议使用一些去抖动) - 使用权
viewState
- 使用类
getBounds
中的方法WebMercatorViewport
import DeckGL, {WebMercatorViewport} from 'deck.gl';
function debounce(fn, ms) {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => {
timer = null;
fn.apply(this, args);
}, ms);
};
}
function handleViewStateChange({ viewState }) {
const bounds = new WebMercatorViewport(viewState).getBounds();
console.log(bounds);
};
<DeckGL
...
onViewStateChange={debounce(handleViewStateChange, 500)}
/>
推荐阅读
- python - 如何在 Django 框架中定期运行 python 代码(不在视图中)?
- python - 我们如何处理合并数据框中的欺骗?
- java - Spring security 不允许我从移动模拟器调用端点
- javascript - 字符串“对话”日期格式到 JavaScript 中的日期格式
- lua - 将 2d 边界转移到其 1d 网格上
- ruby-on-rails - 根据环境传递不同的符号作为参数
- c# - 如何根据 .NET Core 3.1 中的最佳实践与基于标头的标头交换基于声明的安全性?
- curl - Rundeck:无法使用 jobIdFilter 查询选项卷曲 rundeck api
- c# - 无法在 Visual Studio 2019 中创建 C# Holographic 项目
- python - Flask API Post 字典列表