reactjs 当一个Map框图层可见时,禁用在另一个Map框图层中单击

abithluo  于 2022-12-03  发布在  React
关注(0)|答案(1)|浏览(127)

bounty将在2天后过期。回答此问题可获得+150声望奖励。codebot希望吸引更多人关注此问题。

在一个react应用程序里的mapboxMap里有两个图层。我都是新手。我想在一个图层可见的时候禁用另一个图层的点击。我通过react从复选框值控制每个图层的可见性。
我有这个密码

export default function Map () {   
  const [checkedLayer, setCheckedLayer] = useState(true);
  const [checkedOtherLayer, setCheckedOtherLayer] = useState(true);

  //when a checkbox is on or off make layer visible or not
  const handleVisibility = (event) => { 
    let visible ; let layer;  
    event.target.checked ? visible = 'visible' : visible = 'none'
    
    if (event.target.id === 'mylayer') { 
      setCheckedLayer(event.target.checked);   
      layer = 'mylayer';
    } 

    if (event.target.id === 'myotherlayer') { 
      setCheckedOtherLayer(event.target.checked);   
      layer = 'myotherlayer';
    } 

    map.current.setLayoutProperty(layer, 'visibility', visible); 
  }; 

useEffect(() => { 
  if (map.current) return; // initialize map only once  
   map.current = new mapboxgl.Map({
    container: mapContainer.current, 
    style: 'mapbox://styles/mapbox/satellite-streets-v11',
    center: [lng, lat],
    zoom: zoom
  });//map  

  map.current.on('load', () => { 
     //...set layers and sources 
  });//load 

  map.current.on('click', 'mylayer', (e) => {
    console.log('other layer visibility ', map.current.getLayoutProperty('myotherlayer', 'visibility'));
   // I want to do something like 
   // if map.current.getLayoutProperty('myotherlayer', 'visibility')) == 'visible' 
   // return
   // but the above console.log keeps giving "undefined"
   // map.current.getLayoutProperty('mylayer', 'visibility') works
  }); 
     },[checkedLayer, checkedOtherLayer]);//useEffect 

  return (
    <div> 
              <Checkbox
                id="mylayer"
                checked={checkedLayer}
                onChange={handleVisibility}
                inputProps={{ 'aria-label': 'controlled' }}
              />
              <Checkbox
                id="myotherlayer"
                checked={checkedOtherLayer}
                onChange={handleVisibility}
                inputProps={{ 'aria-label': 'controlled' }}
              />
      <div ref={mapContainer} className="map-container"> </div>
    </div>
  )
}

当点击mylayer时,我尝试检查myotherlayer的可见性值,如果它是visible,则返回,因此在'click', 'mylayer'函数中不执行任何操作。但map.current.getLayoutProperty('myotherlayer', 'visibility'));的值是undefined
我哪里做错了?
谢谢

m1m5dgzv

m1m5dgzv1#

您似乎正在尝试访问map.current.on('load ',...)回调函数外部的map.current.getLayoutProperty('myotherlayer','visibility')的值。这可能会导致该值未定义,因为尚未调用map.current.on('load',...)回调函数,因此图层尚未添加到Map中。
您可以通过将检查map.current.getLayoutProperty('myotherlayer ',' visibility')值的代码移动到map.current.on('load ',...)回调函数中来修复此问题,以便在将图层添加到Map后执行该代码。
下面是一个示例,说明如何更新代码以实现此目的:

export default function Map () {
  // Other code here

  useEffect(() => { 
    if (map.current) return; // initialize map only once

    // Initialize the map
    map.current = new mapboxgl.Map({
      container: mapContainer.current, 
      style: 'mapbox://styles/mapbox/satellite-streets-v11',
      center: [lng, lat],
      zoom: zoom
    });

    // Add event listener for when the map is loaded
    map.current.on('load', () => { 
      // Add layers and sources

      // Add event listener for when 'mylayer' is clicked
      map.current.on('click', 'mylayer', (e) => {
        // Check the visibility of 'myotherlayer'
        if (map.current.getLayoutProperty('myotherlayer', 'visibility') == 'visible') {
          // 'myotherlayer' is visible, so return
          return;
        }

        // Do something when 'mylayer' is clicked
      });
    });
  },[checkedLayer, checkedOtherLayer]);

  // Other code here
}

相关问题