简体   繁体   English

react-leaflet 获取当前缩放边界

[英]react-leaflet get current zoom boundaries

I currently have this map我目前有这个 map

import React, { useEffect } from "react";

import "./App.css";
import { MapContainer, TileLayer, Marker } from "react-leaflet";

function App() {
  useEffect(() => {
    console.log("first run");
    
  }, []);



  return (
    <div className="App">
      <div id="mapid">
        <MapContainer

          style={{ height: "100vh", width: "100vw" }}
          center={[51.505, -0.09]}
          zoom={13}
          scrollWheelZoom={true}
        >
          <TileLayer
            attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
            url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
          />
          <Marker position={[51.505, -0.09]}></Marker>
          <Marker position={[51.490114, -0.09066]}></Marker>
        </MapContainer>
      </div>
    </div>
  );
}

export default App;

And i somehow need to find the latitude min, latitude max, longitude min, longitude max我不知何故需要找到最小纬度,最大纬度,最小经度,最大经度

But i cannot find how to do it with hooks / useeffect但我找不到如何使用钩子/useeffect

After taking the map reference, when the component mounts, you can get the map bounds using map.getBounds() .获取 map 参考后,当组件安装时,您可以使用map.getBounds()获取 map 边界。 It contains the current or the updated coordinates of southWest, southEast, northWest, northEast etc.它包含当前或更新的西南、东南、西北、东北等坐标。

 useEffect(() => {
    if (!map) return;
    console.log(map.getBounds());
  }, [map]);

and if you want to take the new ones after you zoom in or out you can add a zoomend event如果你想在放大或缩小后使用新的,你可以添加一个zoomend事件

    useEffect(() => {
        if (!map) return;
        console.log(map.getBounds());
    
        map.on("zoomend", function () {
          console.log(map.getBounds());
        });

  }, [map]);

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM