How to determine the map center prior to initializing map?

746 views
Skip to first unread message

Jason Ho

unread,
May 7, 2014, 6:20:44 PM5/7/14
to leafl...@googlegroups.com
Hi All

I'm trying to dynamically load a large number of maps which are being served via Geoserver where the user will be able to select from these map selections. My main question is that it appears that you are required to specify the center coordinates and zoom level in order to initialize a leaflet map? If I am pulling my map data from geoserver and creating a a TileLayer.WMS layer first, is there anyway to determine the center for this map programatically from the dataset loaded through leaflet?

If I try to do something like

map = new L.Map('map');

it errors with "Error: Set map center and zoom first."

And I was hoping I wouldn't have to go through each map to determine the center by hand, then store it alongside it in the database and return it back?

Thanks in advance

Jason

k_man_au

unread,
May 14, 2014, 9:03:22 PM5/14/14
to leafl...@googlegroups.com
Couple of suggestions:-

- Initialise map at some random center (your home town?) and zoom level. Load your data and then call fitBounds or setView once the data is loaded.

- Calculate a center from the bounds of the tiles (does not need to be exact) then use php or similar to construct the javascript used to initialise the map... 

eg:

var mapcenter = new L.LatLng(<?php echo($row[x] . ", " . $row[y]); ?>);

var map = new L.Map('map', {center: mapcenter, zoom: <?php echo($row[zoomlevel]); ?>});

Hope that is the start of an idea for you.... 
  

Jason Ho

unread,
May 16, 2014, 1:52:52 PM5/16/14
to leafl...@googlegroups.com
Hey there.. thanks for the tips...this is pretty much what I ended up doing for it.

Arnie Shore

unread,
May 16, 2014, 2:08:39 PM5/16/14
to leafl...@googlegroups.com

Consider using the user's location as that start point.

--

---
You received this message because you are subscribed to the Google Groups "Leaflet" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leaflet-js+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages