【问题标题】:Leaflet React get map instance in functional componentLeaflet React在功能组件中获取地图实例
【发布时间】:2021-02-19 04:43:55
【问题描述】:

我想在地图外有一个按钮,可以将视图更改为另一个坐标。

有没有办法让 mapContainer 实例调用它们的函数?或者我该如何实现这个功能?

我试图通过使用 ref 来获取它,但它不起作用。 这是我当前的代码

const zoom = 13;

function Map({ regionCoord, regionName }) {

    const mapRef = useRef();

    function handleFlyToClick() {
      // This don't work
      // const map = mapRef.current.leafletElement 
      // map.flyTo(regionCoord, zoom)
    }

 return (   
        <React.Fragment>
            <Grid container >
                <Grid item xs={10}>
                    {regionCoord && <MapContainer
                        ref={mapRef}                     
                        center={[50,50]} 
                        zoom={zoom}                    
                        >
                        <TileLayer
                            attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
                            url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
                        />            
   
                        <Marker position={regionCoord}>
                          <Popup>{regionName}</Popup>
                        </Marker>        
                    </MapContainer>}                               
                </Grid>
                <Grid item xs={2}>
                    <button onClick={handleFlyToClick}>Fly To</button>
                </Grid>
            </Grid>
        </React.Fragment>  
    )
    
}

export default Map

我正在使用 react-leaflet v3

【问题讨论】:

    标签: javascript reactjs leaflet react-leaflet


    【解决方案1】:

    您需要使用一个包含您的按钮的组件。要获取地图实例,请使用 MapContainerwhenCreated 属性。我认为mapRef 在最新版本中不再有效。

    地图容器:

     const [map, setMap] = useState(null);
    
     <MapContainer
          center={[50, 50]}
          zoom={zoom}
          style={{ height: "90vh" }}
          whenCreated={setMap}
      >
    ...
    
    </MapContainer>
    <FlyToButton />  // use the button here outside of the MapContainer
    
    ....
    

    使用按钮及其事件创建组件

    function FlyToButton() {
      const onClick = () => map.flyTo(regionCoord, zoom);
        
      return <button onClick={onClick}>Add marker on click</button>;
    }
    

    Demo

    【讨论】:

    • 努力寻找如何获取地图实例 (whenCreated={setMap}) 救了我的命。谢谢
    • 希望这在文档中!
    【解决方案2】:

    您需要访问地图元素(来自地图组件,它是容器而不是 MapContainer),这是一个非常简单的示例:

    export default function MapComponent() {
        const [mapCenter,setMapCenter] = useState([13.1538432,30.2154278])
        let [zoom,setZoomLevel] = useState(15)
        let [tile,setTile] = useState('https://tiles.stadiamaps.com/tiles/alidade_smooth_dark/{z}/{x}/{y}{r}.png')
        
        let mapRef = useRef();
    
        const fly=()=>{
            mapRef.current.leafletElement.flyTo([14,30],15)
        }
    
        return (
        <>
            <button onClick={fly}>Click</button>
            <Map center={mapCenter} zoom={zoom} ref={mapRef} style={{width:'100%',height:'100%'}}>
                <TileLayer url={tile}/>
            </Map>
        </>  
        )
    }
    

    【讨论】:

    • 我在发布此帖子后看到了@kboul 的回答,请注意我使用的是 react-leaflet 2.8.0。
    • 这不再适用于 Leaflet 3.0。方法如此处接受的答案所述,使用
    猜你喜欢
    • 2021-06-23
    • 2021-02-08
    • 2020-06-25
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 2020-05-01
    • 2018-05-05
    • 1970-01-01
    相关资源
    最近更新 更多