如何找到 Leaflet 地图显示的纬度和经度范围?

huangapple go评论64阅读模式
英文:

How do I find the latitude and longitude range shown by my leaflet map?

问题

我正在使用浏览器中的JavaScript fetch从我的API获取地理数据点,并将它们填充到一个Leaflet.JS地图中。

目前,我的API在每个请求的响应中全局返回所有标记,这对于测试来说是可以的,但显然在有很多标记时会变得低效。

我想通过位置来限制请求的范围,使其只返回用户相关的标记(即那些显示在他们当前查看的地图区域上的标记)。在后端这样做是可以的,但我找不到如何获取显示在地图上的纬度/经度坐标范围的方法。当地图平移或缩放时,地图应该请求新的数据。

理想情况下,应该有功能来加载比显示区域更宽范围的数据(例如扩大3倍),以允许一些平移/缩放而无需从服务器请求更多数据,只有当平移/缩放扩展到超出该区域时才会发出另一个请求。

这似乎是一个常见的用例,但我找不到任何示例。

我可以找到许多关于从点击事件获取纬度和经度的示例,但我想知道左上角和右下角的纬度/经度,最好找到一些有用的提示,了解如何以明智的方式请求这些数据。

英文:

I am using javascript fetch in the browser to pull geographical datapoints from my API and populate them to a Leaflet.JS map.

At the moment my API is returning all the markers globally in response to every request, which is fine for testing but will obviously become inefficient once there are many markers.

I want to limit the scope of the request by location such that it just returns the relevant markers for the user (ie those which are shown on the map area they are currently viewing). This is fine on the backend, but I can't find out how to get the lat/lon coordinate range that is shown on the map. The map should request new data when it is panned or zoomed.

Ideally there would be functionality to load a wider range of data than the shown area (ex 3x) to allow for some panning/zooming without requesting more data from the server, and only make another request then the pan/zoom expands beyond that area.

This seems like a common use case but I couldn't find any examples.

I could find numerous examples for getting lat and lon from a click event, but I want to know the top left and bottom right lat/lon, and ideally find some useful tips for how to request this data in a sensible way.

答案1

得分: 2

你可以使用getBounds方法,该方法将返回包含你寻找的两个角的坐标的LatLngBounds

英文:

Answer from my comment:
You can use getBounds method which will return LatLngBounds which contain coordinates of both corners you are looking for.

答案2

得分: 0

感谢@zerdox指导我找到正确的方向。 "Bounds" 是我正在寻找的术语。

发布我的工作代码:

// 允许地图在平移时更新
let last_map_update = 0;
const throttle_ms = 500;
map.on("move", function () {
	if ((Date.now() - last_map_update) > throttle_ms) {
		update_map_data();
		last_map_update = Date.now();
	}
});

function update_map_data() {
	let map_bounds = map.getBounds();
	let northEast = map_bounds['_northEast'];
	let southWest = map_bounds['_southWest'];

	fetch(api_url + '/api/map?' + new URLSearchParams({
		'northEastLat': northEast['lat'],
		'northEastLon': northEast['lng'],
		'southWestLat': southWest['lat'],
		'southWestLon': southWest['lng'],
	}), {
		method: 'GET',
	})
		.then(response => response.json())
		.then(data => {
			units_to_show_on_map = data.units;
			show_map_markers();
		})
		.catch(rejected => {
			console.log(rejected);
		});
}

注意:代码中的变量和函数名未进行翻译。

英文:

Thank you to @zerdox for pointing me in the right direction. "Bounds" is the term I was looking for.

Posting my working code:

// Allow the map to update as it is panned
let last_map_update = 0
const throttle_ms = 500
map.on("move", function () {
	if ((Date.now() - last_map_update) > throttle_ms) {
		update_map_data()
		last_map_update = Date.now()
	}
});

function update_map_data() {
	let map_bounds = map.getBounds();
	let northEast = map_bounds['_northEast']
	let southWest = map_bounds['_southWest']

	fetch(api_url + '/api/map?'+ new URLSearchParams({
		'northEastLat': northEast['lat'],
		'northEastLon': northEast['lng'],
		'southWestLat': southWest['lat'],
		'southWestLon': southWest['lng'],
	}), {
		method: 'GET',
	})
		.then(response => response.json())
		.then(data => {
			units_to_show_on_map = data.units
			show_map_markers()
		})
		.catch(rejected => {
			console.log(rejected)
		})
}

huangapple
  • 本文由 发表于 2023年3月1日 15:27:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定