简体   繁体   English

如何使用 getBounds() 获取 map 以 react-leaflet 结束?

[英]How to use getBounds() to get pick up map ends with react-leaflet?

How to get the 4 coordinates that represent the 4 ends visible on the map using react-leaflet .如何使用react-leaflet获取表示 map 上可见的 4 个末端的 4 个坐标。

I would like to use the contains() function to return true or false for a coordinate that is passed, if it is true it means that it is within the limits established in getBounds() , if it is false it is outside.我想使用contains() function 为传递的坐标返回 true 或 false,如果它是 true 则意味着它在getBounds()中建立的限制内,如果它是 false 它在外面。

Just wiht leaflet in pure JavaScript I know how to use this function. But I'm developing a React Web Application, and I'm using react-leaflet library and need use this function.就在纯leaflet中的 leaflet 我知道如何使用这个 function。但是我正在开发一个 React Web 应用程序,我正在使用react-leaflet库并且需要使用这个 function。

Thank you in advance for any help.预先感谢您的任何帮助。

Here's my code I put into codesandbox这是我放入codesandbox的代码

 import React from "react"; import { Map, TileLayer } from "react-leaflet"; import "./styles.css"; const App = () => { const center = [51.505, -0.09]; const [map, setMap] = React.useState(null); React.useEffect(() => { if (map) { const contains = map.getBounds().contains(center); console.log("contains:: ", contains); } }, [center, map]); return ( <Map ref={setMap} style={{ width: "100%", height: "100vh" }} center={center} zoom={13} > <TileLayer url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' /> </Map> ); }; export default App;

You need to get a reference to the map. Then you can access the ref's .leafletElement property, which is the map instance:您需要获得对 map 的引用。然后您可以访问 ref 的.leafletElement属性,即 map 实例:

const App = ({ center }) => {
  const mapRef = useRef();

  React.useEffect(() => {
    if (mapRef) {
      const contains = mapRef.leafletElement.getBounds().contains(center);
    }
  }, [center]);

  return (
    <Map 
      ref={mapRef}
      {...otherProps}
    >
      <TileLayer {...props} />
    </Map>
  );
};

Working codesandbox 工作代码和盒子


Modern answer for react-leaflet v4 react-leaflet v4 的现代答案

You need a reference to the map, so that you can call things like L.map.getBounds() .您需要对 map 的引用,以便您可以调用L.map.getBounds()类的东西。 react-leaflet no longer uses the .leafletElement property since react-leaflet v3. react-leaflet 从 react-leaflet v3 开始不再使用.leafletElement属性。 Here's a v4 answer:这是一个 v4 答案:

const App = ({ center }) => {
  const [map, setMap] = useState(null)

  React.useEffect(() => {
    if (map) {
      const contains = map.getBounds().contains(center);
    }
  }, [center]);

  return (
    <Map 
      ref={setMap}
      {...otherProps}
    >
      <TileLayer {...props} />
    </Map>
  );
};

The docs have some examples of how to grab the underlying L.map instance as a ref and use it, here: https://react-leaflet.js.org/docs/example-external-state/ .文档有一些示例,说明如何获取底层 L.map 实例作为引用并使用它,此处: https://react-leaflet.js.org/docs/example-external-state/

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

相关问题 如何在 react-leaflet 中调用 fitBounds 和 getBounds? - How to call fitBounds and getBounds in react-leaflet? 如何在缩放更改事件的反应传单中获取 Map object - How to get Map object in react-leaflet on zoom change event 如何根据react-leaflet中的选定位置flyTo然后在位置周围获取Bounds - How to flyTo and then getBounds around location based on selected location in react-leaflet 如何获取反应传单 map 的边框并检查 map 内的标记? - How to get borders for the react-leaflet map and check markers inside the map? 如何将 react-leaflet map 定位到用户当前的 position 并获取此 map 的边框? - How to locate react-leaflet map to user's current position and get the borders for this map? 如何在 react-leaflet 中使用带有 OverlappingMarkerSpiderfier 的 Leaflet 的 FeatureGroup? - How to use Leaflet's FeatureGroup with OverlappingMarkerSpiderfier in react-leaflet? 关于如何在 reactjs 和 react-leaflet 中使用传单的折线的指针 - Pointers on how to use leaflet's polyline in reactjs & react-leaflet 如何在 React-Leaflet 3 中使用 Leaflet 路由机? - How to use Leaflet Routing Machine with React-Leaflet 3? React-Leaflet map 未在 map 上呈现数据 - React-Leaflet map is not rendering data on map 我应该如何将 OverlappingMarkerSpiderfier 与 react-leaflet 一起使用? - How should I use OverlappingMarkerSpiderfier with react-leaflet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM