javascript - How to set default view in OpenLayers, WITHOUT restricting bounds? - Stack Overflow

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

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.

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.

Share Improve this question edited Dec 31, 2010 at 19:49 winwaed 7,8296 gold badges42 silver badges84 bronze badges asked Aug 24, 2010 at 13:33 J.R.J.R. 6,06912 gold badges57 silver badges79 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745555342a4632786.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信