如何在 OpenLayers 中设置默认视图而不限制边界?

发布于 2024-09-15 14:51:02 字数 765 浏览 2 评论 0原文

根据示例,我可以看到您可以通过以下方式在 OpenLayers 中设置默认视图:

    var bounds = new OpenLayers.Bounds(-125, 25, -65, 50);
var map = new OpenLayers.Map('map', {restrictedExtent: bounds  });

但是,这也(顾名思义)限制我只能在这些范围内导航。我可以缩小并看到这些边界之外的东西,但我不能再放大到它们,或滚动到它们。

我尝试过不设置任何restrictedExtent(或使其成为整个地图),但后来我无法让它专注于我想要的区域。我尝试使用:

     map.setCenter(new OpenLayers.LonLat(0,0), 3);
     console.log(map.getCenter());

设置缩放和中心...但它似乎没有做任何事情,除了设置变量“center”,然后我可以从map.getCenter()读取它(如果我不设置它,它是空的,如果我设置它,我可以看到它...但是地图本身保持完全扩展,并且它的中心似乎根本没有改变...)

我使用的地图图层是:

OpenLayers.Layer.OSM.Mapnik

with displayOutsideMaxExtent设置为 true...我真的很茫然。

我的目标是放大并查看世界的默认区域(例如美国),并且观看者可以选择在默认区域之外查看事物。

Based on samples, I can see that you can set a default view in OpenLayers by saying something along the lines of:

    var bounds = new OpenLayers.Bounds(-125, 25, -65, 50);
var map = new OpenLayers.Map('map', {restrictedExtent: bounds  });

However, this also (as the name implies), restricts me to be able to ONLY navigate within these bounds. I can zoom out and see things outside of these bounds, but I can't then zoom back onto them, or scroll to them.

I've tried not having any restrictedExtent (or making it the entire map), but then I can't get it to focus on the area I want. I tried using:

     map.setCenter(new OpenLayers.LonLat(0,0), 3);
     console.log(map.getCenter());

To set the zoom and the center...but it doesn't seem to do ANYTHING, other than set the variable "center" which I can then read from map.getCenter() (if I don't set it, it's null, if I do set it, I can see it...but the map itself stays fully extended and it's center doesn't seem to change at all...)

The Map layer I am using is:

OpenLayers.Layer.OSM.Mapnik

with displayOutsideMaxExtent set to true... I'm really at a loss here.

My goal is to have a default region of the world zoomed in to and in view (such as the United States), with the option of viewers being able to go outside the default to view things.

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

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

发布评论

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

评论(1

咋地 2024-09-22 14:51:02

我想我已经明白了。无论出于何种原因,变焦从未改变,但中心显然在移动(它被缩小得我无法分辨)。再加上我需要改造中心以使用谷歌投影,它似乎工作得很好。

我最终做的是:

var lonlat = new OpenLayers.LonLat(20,37).transform(new OpenLayers.Projection("EPSG:4326"),new OpenLayers.Projection("EPSG:900913"));
map.setCenter(lonlat);
map.zoomTo(4);

I think I've figured it out. For whatever reason, the zoom was never changing, but the center apparently WAS moving (it was just so zoomed out I couldn't tell). Add to the fact that I needed to transform the center to use the google projection, and it seems to work just fine.

What I ended up doing was:

var lonlat = new OpenLayers.LonLat(20,37).transform(new OpenLayers.Projection("EPSG:4326"),new OpenLayers.Projection("EPSG:900913"));
map.setCenter(lonlat);
map.zoomTo(4);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文