简体   繁体   English

反应 leaflet map 得到角纬度和长

[英]React leaflet map get corners lat and long

Im working with react-leaflet and Im trying to do the following thing:我正在使用 react-leaflet 并尝试做以下事情:

In my map I need to show pointers (lat,long) that are in the database, to do this I have to call the backend and get the pointers, the problem is that there are a lot of pointers, and get all of theme in a single call could generate problems.在我的 map 中,我需要显示数据库中的指针(纬度,经度),为此我必须调用后端并获取指针,问题是有很多指针,并获取所有主题一个电话可能会产生问题。

My idea is that depending the position of the user in the map, make a call to the backend that only get the pointers related to the quadrant that the user is watching, and only call the backend when the zoom of the map is close to the surface.我的想法是,根据 map 中用户的 position,调用后端仅获取与用户正在观看的象限相关的指针,并且仅在 Z1D78DC8ED41214E59AEZB51 的缩放接近时调用后端表面。

To achieve this I need two things, the first one is get the (longitude, latitude) of every corner of what the user is watching in the map, the second one is get the zoom (so, I only execute the backend call is the zoom is bigger than 15).为了实现这一点,我需要两件事,第一件事是获取用户在 map 中观看的每个角落的(经度,纬度),第二件事是获取缩放(所以,我只执行后端调用是缩放大于 15)。

So for example, when the zoom of the map is 15 and the corners are:因此,例如,当 map 的缩放为 15 并且角为:

corner_top_left: [40.06957696,-105.10070800781251]角顶左:[40.06957696,-105.10070800781251]

corner_top_right: [40.06957696,-104.30969238281251]角顶右:[40.06957696,-104.30969238281251]

corner_bottom_left: [39.81477752660833,-105.10070800781251]角底左:[39.81477752660833,-105.10070800781251]

corner_bottom_right: [39.81477752660833,-104.30969238281251]角底右:[39.81477752660833,-104.30969238281251]

I could generate a request that get only the pointers that belongs there.我可以生成一个只获取属于那里的指针的请求。

Is there a way to achieve to get this data?有没有办法获得这些数据? Do you think that theres a better way to resolve this problem?你认为有更好的方法来解决这个问题吗? thanks for answers.感谢您的回答。

This is an example of the code used to work with maps, the following code have nothing to do with the problem, is just to let you know how I work with the map.这是用于处理地图的代码示例,以下代码与问题无关,只是为了让您了解我如何使用 map。

 import React from 'react'
import { MapContainer, Marker, TileLayer, useMapEvents } from 'react-leaflet'
import { iconMap } from '../../assets/customeIcon/iconMap';
import 'leaflet/dist/leaflet.css'



const MapView = ({ selectedPosition, setSelectedPosition, editable }) => {

    

    const [initialPosition, setInitialPosition] = React.useState([38, -101]); 
 

    const Markers = () => {

        const map = useMapEvents({
            click(e) {
                if(editable){
                setSelectedPosition([
                    e.latlng.lat,
                    e.latlng.lng
                ]);
            }
            },
        })

        return (
            selectedPosition ?
                <Marker
                    key={selectedPosition[0]}
                    position={selectedPosition}
                    interactive={false}
                    icon={iconMap}
                />
                : null
        )

    }



    return  <MapContainer center={selectedPosition || initialPosition} zoom={editable? 5:15} >
        <TileLayer url='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'
        ></TileLayer>
      <Markers />         
    </MapContainer>
}

export default MapView

This can be done, and with react-leaflet v3's new event handling, it can be done nicely in its own component.这可以做到,并且使用 react-leaflet v3 的新事件处理,它可以在它自己的组件中很好地完成。 What you need first is a reference to the underlying L.map , and you need to hook into the map's native events.您首先需要的是对底层L.map的引用,并且您需要挂钩到地图的原生事件。 You can do this with RLV3's new useMapEvents hook:您可以使用 RLV3 的新useMapEvents钩子来做到这一点:

function makeACall(bounds, zoom, zoomThreshold = 8) {
  if (zoom > zoomThreshold) {
    fetch('your.endpoint')
      .then(res => res.json())
      .then(res => setMarkersArray(res.markers))
  }
}

const MapEvents = () => {
  const map = useMapEvents({
    moveend: () => makeACall(map.getBounds(), map.getZoom()),
    zoomend: () => makeACall(map.getBounds(), map.getZoom())
  });
  return null;
};

Then include <MapEvents /> as a child of the MapContainer .然后将<MapEvents />包含为MapContainer的子项。 Now your map will fetch the markers on every zoomend or moveend .现在您的 map 将获取每个zoomendmoveend上的标记。 This format I showed you assumes the marker data comes in the form of an array of latlngs , and you can .map over those to render them in the MapContainer .我向您展示的这种格式假定标记数据以latlngs数组的形式出现,您可以.map将它们呈现在MapContainer中。

Working codesandbox工作代码框

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

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