如何计算java中边界框的谷歌地图缩放级别

发布于 2024-10-04 03:46:34 字数 44 浏览 0 评论 0原文

我需要获取服务器端边界框的谷歌地图缩放级别(用java编码)有什么想法吗?

I need to get the google maps zoom level for a bounding box in the server side (that is coded in java) any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情定在深秋 2024-10-11 03:46:34

@Peter实际上你可以使用Java在服务器端做一些事情。您可以根据边界框计算最小和最大纬度/经度值,然后使用以下代码:

int zoomLevel;
double latDiff = latMax - latMin;
double lngDiff = lngMax - lngMin;

double maxDiff = (lngDiff > latDiff) ? lngDiff : latDiff;
if (maxDiff < 360 / Math.pow(2, 20)) {
    zoomLevel = 21;
} else {
    zoomLevel = (int) (-1*( (Math.log(maxDiff)/Math.log(2)) - (Math.log(360)/Math.log(2))));
    if (zoomLevel < 1)
        zoomLevel = 1;
}

这对于我的 320x320 像素的方形 Google 地图(ZOOM_SCALING_FACTOR = 0.3838)效果很好。这个公式应该适用于任何方形谷歌地图。您看到 log_2 被使用的原因是,每当缩放级别增加 1 时,Google 地图的大小就会加倍。我还处理边缘情况,其中点几乎彼此重合 (zoomLevel = 21),以及整个地球是所需的缩放(zoomLevel = 1)。

在服务器端进行此操作的好处是,您可以将 JavaScript 引擎从执行复杂(且可能浪费)的数学中解放出来,这可能会减慢页面加载速度并恶化用户体验。

@Peter Actually there is something you can do on the server side using Java. You can calculate the minimum and maximum latitude/longitude values from your bounding box, and then use the following code:

int zoomLevel;
double latDiff = latMax - latMin;
double lngDiff = lngMax - lngMin;

double maxDiff = (lngDiff > latDiff) ? lngDiff : latDiff;
if (maxDiff < 360 / Math.pow(2, 20)) {
    zoomLevel = 21;
} else {
    zoomLevel = (int) (-1*( (Math.log(maxDiff)/Math.log(2)) - (Math.log(360)/Math.log(2))));
    if (zoomLevel < 1)
        zoomLevel = 1;
}

This worked well for my square Google map of 320x320 pixels, with ZOOM_SCALING_FACTOR = 0.3838. This formula should work correctly for ANY square Google map. The reason you see log_2 being used is that a Google map doubles in size every time the zoom level increases by 1. I also handle the edge cases where the points are almost coincident with each other (zoomLevel = 21), and where the entire globe is the desired zoom (zoomLevel = 1).

The nice thing about doing this server side is that you have freed your JavaScript engine from doing complex (and potentially wasteful) math which could slow down the page load and worsen the user experience.

快乐很简单 2024-10-11 03:46:34

Google 地图是通过在客户端(浏览器内)运行的 JavaScript 库使用的。您在服务器端无能为力。

但是,您可以查看 javascript 中的边界框大小(在客户端)并相应地设置缩放级别。

我很好奇:你在服务器端用谷歌地图做什么?

Google maps is used via a javascript library that runs on the client side (inside browser). There is nothing you can do on the server side.

You can however look at the bounding box size in javascript (on the client side) and set zoom level accordingly.

I'm curious: what are you doing with google maps on the server side?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文