map.setMaxBounds(map.getBounds()); and general maxBounds issues

2,338 views
Skip to first unread message

sen

unread,
Jul 24, 2012, 10:47:20 AM7/24/12
to leafl...@googlegroups.com
Does anyone know how to fix this?  I'm using leaflet 0.3.1 stable. If you run the following code

=============================
<div id="map" style="height:600px;width:1000px;"></div>
    
<script>
var map = new L.Map('map');
       
   cloudmadeAttribution = 'Map data &copy; 2011 OpenStreetMap contributors, Imagery &copy; 2011 CloudMade',
   cloudmade = new L.TileLayer(cloudmadeUrl, {maxZoom: 18, attribution: cloudmadeAttribution});

var usaCenterPoint = new L.LatLng(49.15296965617039, -111.97265625);
map.setView(usaCenterPoint, 3).addLayer(cloudmade);
</script>
=============================

You should get a fairly well centered map that includes the 48 contiguous states + Alaska and Hawaii.

If you then add this line to restrict the user from shifting from this view

map.setMaxBounds(map.getBounds());

it throws it off, seems to affect the zoom level.  I've also tried manually recording the southwest and northeast corner latlngs and creating the max bounds that way... then I always end up in Europe.  Example :

=============================
<div id="map" style="height:600px;width:1000px;"></div>
    
<script>
var map = new L.Map('map');
       
 cloudmadeAttribution = 'Map data &copy; 2011 OpenStreetMap contributors, Imagery &copy; 2011 CloudMade',
 cloudmade = new L.TileLayer(cloudmadeUrl, {maxZoom: 18, attribution: cloudmadeAttribution});

var usaCenterPoint = new L.LatLng(49.15296965617039, -111.97265625);
var southWest = new L.LatLng(3.8642546157214084, -199.86328125);
var northEast = new L.LatLng(73.12494524712693 , -24.08203125);

var bounds = new L.LatLngBounds(southWest, northEast);
      
map.setView(usaCenterPoint, 1).addLayer(cloudmade);
map.setMaxBounds(bounds);
</script>
=============================

Any suggestions ?  Thanks

Vladimir Agafonkin

unread,
Jul 24, 2012, 11:46:16 AM7/24/12
to leafl...@googlegroups.com
The problem is here: 
new L.LatLng(3.8642546157214084, -199.86328125); 

LatLng wraps the -199 longitude. You can disable this by passing true as a third argument.

I'm thinking about solving this issue more gracefully for next versions though.

2012/7/24 sen <seneca...@gmail.com>
--
 
 
 



--
Vladimir Agafonkin
http://agafonkin.com/en
+380 (93) 745 44 61
Reply all
Reply to author
Forward
0 new messages