为什么不使用Mapbox删除监听器

问题描述

我正在使用react-map-gl,并且正在尝试使层的bBox适应边界。

我使用moveend动作在完成fitbounds时保存纬度和经度,并调用删除侦听器,但这不起作用,我也不知道为什么。

如何解决

const updateViewportOnFinishFly = (mapGL: any) => {
    if (!mapGL) return;
    mapGL.off("moveend");
    const centerMap = center(leaksLocationMapState.topologyData);
    const latitude: number = centerMap?.geometry?.coordinates[1];
    const longitude: number = centerMap?.geometry?.coordinates[0];
    const zoom: number = mapGL.getZoom();
    if (isNaN(latitude) || isNaN(longitude) || isNaN(zoom)) return;
    setLeaksLocationMapState({
        ...leaksLocationMapState,viewport: {
            width: window.screen.width,height: height,latitude: latitude,longitude: longitude,zoom: zoom,},});
};

useEffect(() => {
    if (leaksLocationMapState.topologyData?.features?.length > 0) {
        const boundingBox = bBox(leaksLocationMapState.topologyData);
        const mapGL = mapRef?.current;
        if (mapGL !== null) {
            mapGL.getMap().fitBounds(boundingBox,{ padding: 40 });
            mapGL.getMap().on("moveend",() => {
                updateViewportOnFinishFly(mapGL.getMap());
            });
        }
    }
},[]);

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)